Get DataTable from DataGridView

I have one form that is CRUD, this form can manage any data from many tables, if the table has foreign keys, CRUD detected tables and columns for respect columns of the current table, so columns like CheckBox, TextBox or ComboBox can be displayed in DataGridView

I do all this before the DataGridView is populated with data, so I cannot use this:

dataGridView1.DataSource = dtCurrent;

I need something like this:

dtCurrent = dataGridView1.DataSource;

But just give null

I tried using ExtensionMethod in a DataGridView:

public static DataTable ToDataTable(this DataGridView dataGridView, string tableName)
{

    DataGridView dgv = dataGridView;
    DataTable table = new DataTable(tableName);

    // Crea las columnas 
    for (int iCol = 0; iCol < dgv.Columns.Count; iCol++)
    {
        table.Columns.Add(dgv.Columns[iCol].Name);
    }

    /**
      * THIS DOES NOT WORK
      */
    // Agrega las filas 
    /*for (int i = 0; i < dgv.Rows.Count; i++)
    {
        // Obtiene el DataBound de la fila y copia los valores de la fila 
        DataRowView boundRow = (DataRowView)dgv.Rows[i].DataBoundItem;
        var cells = new object[boundRow.Row.ItemArray.Length];
        for (int iCol = 0; iCol < boundRow.Row.ItemArray.Length; iCol++)
        {
            cells[iCol] = boundRow.Row.ItemArray[iCol];
        }

        // Agrega la fila clonada                 
        table.Rows.Add(cells);
    }*/

    /* THIS WORKS BUT... */
    foreach (DataGridViewRow row in dgv.Rows)
    {

        DataRow datarw = table.NewRow();

        for (int iCol = 0; iCol < dgv.Columns.Count; iCol++)
        {
            datarw[iCol] = row.Cells[iCol].Value;
        }

        table.Rows.Add(datarw);
    }

    return table;
} 

I use:

dtCurrent = dataGridView1.ToDataTable(dtCurrent.TableName);

The code does not work if:

int affectedUpdates = dAdapter.Update(dtCurrent);

I get the Duplicate Values ​​Exception (Spanish translation):

, , , , . , , , .

DataGridView DataTable

+5
4

datagrigview datatable.

Dim dt As New DataTable
dt = (DirectCast(DataGridView1.DataSource, DataTable))

datagridview datatable .

+5

DataTable,

 ((DataRowView)DataGridView1.Rows[0].DataBoundItem).DataView.Table 

.

+2

- :

private DataTable GetDataGridViewAsDataTable(DataGridView _DataGridView) 
{
    try {
        if (_DataGridView.ColumnCount == 0) return null;
        DataTable dtSource = new DataTable();
        //////create columns
        foreach(DataGridViewColumn col in _DataGridView.Columns) {
            if (col.ValueType == null) dtSource.Columns.Add(col.Name, typeof(string));
            else dtSource.Columns.Add(col.Name, col.ValueType);
            dtSource.Columns[col.Name].Caption = col.HeaderText;
        }
        ///////insert row data
        foreach(DataGridViewRow row in _DataGridView.Rows) {
            DataRow drNewRow = dtSource.NewRow();
            foreach(DataColumn col in dtSource.Columns) {
                drNewRow[col.ColumnName] = row.Cells[col.ColumnName].Value;
            }
            dtSource.Rows.Add(drNewRow);
        }
        return dtSource;
    }
    catch {
        return null;
    }
}
+1
source

How about this:

DataView dv = (DataView)(dataGridView1.DataSource);

dt = dv.ToTable();

Works for all occasions!

-2
source

All Articles