I am trying to get all the rows that exist in allData but not in removeData
public static DataTable RemoveDuplicateRows(DataTable allData,
DataTable removeData)
{
removeData.Merge(allData);
DataTable newData = removeData.GetChanges();
removeData.RejectChanges();
return newData;
}
removeData is empty prior to the call in this case (just a new DataTable();)
But newData always has a value of null after the DataTable newData = removeData.GetChanges(); line
Final Solution:
public static DataTable RemoveDuplicateRows(DataTable allData, DataTable removeData)
{
DataTable duplicate = allData.Clone();
foreach (DataRow row in allData.Rows)
{
duplicate.ImportRow(row);
}
foreach (DataRow row in duplicate.Rows)
{
row.SetAdded();
}
removeData.Merge(duplicate);
DataTable newData = removeData.GetChanges(DataRowState.Added);
removeData.RejectChanges();
allData.RejectChanges();
return newData;
}
Your
removeDataDataTable needs to have the same columns/fields asallData. In other words, it can’t just be a new DataTable().