1

I have following code:

DataTable datTable3 = new DataTable();
datTable3 = datTable1.Clone();

datTable2.Merge(datTable1);
datTable3 = datTable2.GetChanges();

Want I want to do is: Compare DataTable1 with DataTable2 and when there are rows in DataTable1 which aren't in DataTable 2 then add these rows into a new DataTable(3). This code above gives me an empty DataTable3 each time although the rows in the first dt are not equal to the rows in my second dt. what am I doing wrong? Sorry if that question may be too easy but I'm using C# since a couple of months.

EDIT: I found this solution which doesn't work for me... Why?

 DataTable datTable3 = new DataTable();
        datTable3 = datTable1.Clone();

        foreach (DataRow row in datTable1.Rows)
        {
            datTable3.ImportRow(row);
        }

        foreach (DataRow row in datTable3.Rows)
        {
            row.SetAdded();
        }

        datTable2.Merge(datTable3);
        DataTable datTableFinal = datTable2.GetChanges(DataRowState.Added); 
 // shows me a datatable with again the values from datTable1 
 // even if they are already in datTable2!
        datTable2.RejectChanges();
        datTable1.RejectChanges();
  • possible duplicate of [Compare two DataTables to determine rows in one but not the other](http://stackoverflow.com/questions/164144/compare-two-datatables-to-determine-rows-in-one-but-not-the-other) – Pedro M Duarte Aug 13 '15 at 19:15

2 Answers2

1

The DataTable.GetChanges() method Gets a copy of the DataTable that contains all changes made to it since it was loaded or AcceptChanges was last called.

In other words, GetChanges() is dependent on the DataRow.RowState property. A DataTable.Merge() will either preserve their 'RowState' property, or reset it to 'Unchanged'.

This means that when you merge two DataTables with rows that have 'Unchanged' RowStates, the merged table will also contain 'Unchanged' rows and the DataTable.GetChanges method will return null or Nothing.

EDIT : You can always iterate through the DataTable to see what rows are added to the merged table. Something like

foreach(DataRow row in datTable2.Rows)
{
     Console.WriteLine("--- Row ---"); // Print separator.
    foreach (var item in row.ItemArray) // Loop over the items.
    {
    Console.Write("Item: "); // Print label.
    Console.WriteLine(item); // Invokes ToString abstract method.
    }
}
Saagar Elias Jacky
  • 2,684
  • 2
  • 14
  • 28
  • 1
    @ScorpX Probably not the most performant solution but it will work - http://stackoverflow.com/questions/9289475/difference-of-two-datatables-in-c-sharp. Otherwise you will have to make sure that all rows in the modified table have correct RowState (Added or Modified), that may or may not be possible to achieve with your current design. – Eugene Podskal May 05 '15 at 15:03
  • I need to compare rows, not cell values... that's my problem. I tried the solution from here: http://stackoverflow.com/questions/1702440/datatable-getchanges-keeps-returning-null but even if I updated my database he keeps telling me that all rows were added... I edit that –  May 05 '15 at 15:17
  • @ScorpX check this out : http://stackoverflow.com/questions/7517968/how-to-compare-2-datatables – Saagar Elias Jacky May 05 '15 at 15:36
  • Doesn't work for me... I have a problem with the merge function. Somehow it puts the records in even if they are already in my datatable. So after that I got duplicates... –  May 05 '15 at 16:49
0

Iterate through and use LoadDataRow(object[] value, bool fAcceptChanges) :

foreach (DataRow row in MergeTable.Rows)
{
    TargetTable.LoadDataRow(row.ItemArray, false);
}
var changes = TargetTable.GetChanges();

changes had the desired value when I tried this method.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
JoelC
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 16 '22 at 07:00