c# - .net Compare two DataTable, find modified, added and deleted rows -


i have datatable holds rows modified user ui. after user save changes want check if rows added, deleted or modified , return them.

//dtlast - datatable loaded on open //dtcurrent - datatable loaded on save  datatable dtchanges = null;  dtlast.merge(dtcurrent, true); dtchanges = dtlast.getchanges();  return dtchanges; //return rows added, deleted, modified 

that not work. idea wrong?

//////////////////////////////////////////////

with msdn code wrote this:

using system; using system.collections.generic; using system.data; using system.linq; using system.text; using system.threading.tasks;  namespace consoleapplication1 { class program {     static void main(string[] args)     {         demonstratemergetable();     }      private static void demonstratemergetable()     {         // create new datatable.         datatable table1 = new datatable("items");          // add 2 columns table:         datacolumn column = new datacolumn("id", typeof(system.int32));         column.autoincrement = true;         table1.columns.add(column);          column = new datacolumn("item", typeof(system.string));         table1.columns.add(column);          // set primary key column.         table1.primarykey = new datacolumn[] { table1.columns[0] };          // add rows.         datarow row;         (int = 0; <= 3; i++)         {             row = table1.newrow();             row["item"] = "item " + i;              table1.rows.add(row);         }          // accept changes.         table1.acceptchanges();         printvalues(table1, "original values");         //         //         //           // using same schema original table,          // modify data later merge.         datatable modifiedtable = table1.copy();          modifiedtable.rows[0]["item"] = "new item 0";         modifiedtable.rows[2].delete();         modifiedtable.rows[3].delete();          datarow drrow = modifiedtable.newrow();         drrow["id"] = 2;         drrow["item"] = "new item 2";         modifiedtable.rows.add(drrow);          modifiedtable.acceptchanges();          printvalues(modifiedtable, "modified table");         ////////////////////////////////////         datatable table1copy1 = table1.copy();         datatable table1copy2 = table1.copy();         //datatable table1copy3 = table1.copy();          datatable modifiedtablecopy1 = modifiedtable.copy();         datatable modifiedtablecopy2 = modifiedtable.copy();          ////////////////////////////////         ////////////////////////////////          table1copy1.merge(modifiedtable, true);         printvalues(table1copy1, "table1copy1 after merge true");         //         datatable dtchanges1 = null;         dtchanges1 = table1copy1.getchanges(datarowstate.modified);         printvalues(dtchanges1, "dtchanges1");         //         //         table1copy2.merge(modifiedtable, false);         printvalues(table1copy2, "table1copy2 after merge false");         //         //datatable dtchanges2 = null;         //dtchanges2 = table1copy2.getchanges();         //printvalues(dtchanges2, "dtchanges2");         ////         //         modifiedtablecopy1.merge(table1, true);         printvalues(modifiedtablecopy1, "modifiedtablecopy1 after merge true");         //         datatable dtchanges3 = null;         dtchanges3 = modifiedtablecopy1.getchanges(datarowstate.modified);         printvalues(dtchanges3, "dtchanges3");         //         //         modifiedtablecopy2.merge(table1, false);         printvalues(modifiedtablecopy2, "modifiedtablecopy2 after merge false");         //         //datatable dtchanges4 = null;         //dtchanges4 = modifiedtablecopy2.getchanges();         //printvalues(dtchanges4, "dtchanges4");         ////         //       }       private static void printvalues(datatable table, string label)     {         // display values in supplied datatable:         console.writeline(label);         foreach (datarow row in table.rows)         {             foreach (datacolumn column in table.columns)             {                 console.write("\t{0}  ", row[column, datarowversion.original]);                  console.write("\t{0}  ", row[column, datarowversion.current]);             }             console.writeline();         }     }  } } 

but can see though don't modify row [1 "item 1"] appears in "getchanges()".

right i'm stuck with

        datatable dtchanges = null;         dtcurrent.acceptchanges();          dtcurrent.merge(dtlast, true);         dtchanges = dtcurrent.getchanges(datarowstate.unchanged);          return dtchanges; 


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -