Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetChanges() Or RowState to get all changes of a datatable?

Tags:

c#

winforms

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?

like image 988
M. X Avatar asked Aug 06 '12 14:08

M. X


2 Answers

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
            }
        }
like image 123
rene Avatar answered Sep 22 '22 20:09

rene


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.

like image 43
Reed Copsey Avatar answered Sep 20 '22 20:09

Reed Copsey