Winforms – How to find which rows have been updated in the underlying Datatable on updating the Datagridview

ado.netbindingsourcedata-bindingdatagridviewwinforms

This is what I am trying to achieve:

  1. DataTable populated using data from non database source
  2. This table is bound to a DataGridView using BindingSource
  3. The DataGridView is updated by the user, so that some cells now have new values.
  4. Because the table is bound to the datagridview its values are updated.

How do I get only the updated rows(the rows which have been edited) in the grid/datatable?

I have tried :

DataRow[] updatedRows = 
              _table.Select(null, null, DataViewRowState.ModifiedCurrent);

But this always returns 0 rows. Is there a way to get only the modified rows?

Worst case:

  • Keep a copy of the original table
  • Get the new table from the datagridview datasource
  • Compare all rows.

Thanks!

Best Answer

How about this:

DataTable changedRecordsTable = dataTable1.GetChanges();

You can find more details at How to: Retrieve Changed Rows

Related Topic