Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get DataTable from DataGridView

I have one form that is a CRUD, this form can manage any data from many tables, if a table has foreign keys the CRUD found the tables and columns for respectives columns of the current table, so in the DataGridView can show columns as CheckBox, TextBox or ComboBox

I do all this BEFORE the DataGridView is filled with the data, so I can't use this:

dataGridView1.DataSource = dtCurrent;

I need something like this:

dtCurrent = dataGridView1.DataSource;

But just give a null

I have tried with ExtensionMethod to the 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 works when:

int affectedUpdates = dAdapter.Update(dtCurrent);

I get an Exception about duplicated values (translated from spanish):

The changes requested to the table were not successful because they would create duplicate values in the index, primary key or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.

I just need to Update changes in the DataGridView using DataTable

like image 378
tttony Avatar asked Mar 06 '11 01:03

tttony


People also ask

How to get DataGridView to DataTable in c#?

Converting DataGridView to DataTable When the Convert Button is clicked, first the Columns from the DataGridView are used to create a DataTable and then using a loop, data from each Row of DataGridView is inserted into the DataTable in Windows Forms (WinForms) Application using C# and VB.Net.

What is DataGridView data binding?

The DataGridView control supports the standard Windows Forms data binding model, so it can bind to a variety of data sources. Usually, you bind to a BindingSource that manages the interaction with the data source.

What is DataGridView in C#?

The DataGridView control provides a customizable table for displaying data. The DataGridView class allows customization of cells, rows, columns, and borders through the use of properties such as DefaultCellStyle, ColumnHeadersDefaultCellStyle, CellBorderStyle, and GridColor.


1 Answers

If you need a reference to the actual DataTable source, try this

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

don't forget to process errors.

like image 93
Kakha Middle Or Avatar answered Oct 13 '22 17:10

Kakha Middle Or