In a Winforms application I have to log all changes in datagrid (datatable). In other words I want to to get all changes, since it has been loaded. For this I want to use Datatable.GetChanges(). I know, that with GetChanges() I get a datatable containing a copy of all rows in the original DataSet that have pending changes.
My question is now, if it is also possible to get more additional information of the changes. For example I want to know if a row has been added or deleted or only has been updated. If a row has been updated I also want to know which cells has been updated? Is there a way to get all this information quickly or does I have to do a deep comparison row by row with the original datatable?
Or is it better to use RowState to get all changes?
For the row addition/deletion check RowState
For each item in the row (aka the cell) check DataRowVersion
foreach (DataRow dr in dt.Rows)
{
if (dr.RowState == DataRowState.Modified)
{
var changedCols = new List<DataColumn>();
foreach (DataColumn dc in dt.Columns)
{
if (!dr[dc, DataRowVersion.Original].Equals(
dr[dc, DataRowVersion.Current])) /* skipped Proposed as indicated by a commenter */
{
changedCols.Add(dc);
}
}
// now handle the changedCols list
}
}
Each DataRow
in the resulting DataTable will have its RowState
property set, which tells you whether the row was added, deleted, or updated.
I do not believe individual cells provide update information, however - it's only tracked by row.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With