Link to home
Start Free TrialLog in
Avatar of Jimbo99999
Jimbo99999Flag for United States of America

asked on

VB.Net - Comparing Two DataTables for Differences

Good Day Experts!

I have a little project involving a DataTable that I use to load a DataGrid on my form.  I want to give the User the option when hitting the button to reload the entire DataGrid or to get the differences from the last time the DataGrid was filled.  The DataGrid will either have updated rows or added rows to the previous fill.  

It is "feeling" like I need to compare the 2 DataTables for changes but I am not sure how to do so.  Can you recommend references that may help me with this?

Thanks for your insight,
jimbo99999
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

You can call the DataTable.GetChanges method and it returns you a collection of rows that have changed with the change type

http://msdn.microsoft.com/en-us/library/k2552649.aspx
Avatar of Jimbo99999

ASKER

Thanks for repsonding CodeCruiser.  Can you help me "talk" my way through implimenting your suggestion?

I have my initial DataTable that is used to load the DataGrid.  Then after a period of time, the User will hit the button again.  I am trying to find the differences between the previous DataTable and the new one from when the User hit the button again.  

I am trying to merge the 2 DataTables then do a .GetChanges to see what is new.

               tblOverridesTable.Merge(tblOverridesTable2, False)
                tblDifferencesTable = tblOverridesTable.GetChanges

Instead of merging, the new DataTable contains all records from both DataTables.  
Also, tblDifferencesTable is nothing.

Do you see anything in my idea that is causing trouble?

Thanks,
jimbo99999
Are the changes saved between original fill and user clicking the button?
Good Day

This is a little different.  They are not updating the data in the DataGrid.  The SQL table that supplies the DataGrid gets updates(changes to records and new records).  What I am trying to do is get the changes to that SQL table since the last time the DataGrid was loaded.  

So, the first time they hit the <Get Records> button I fill the DataTable then set it to the DataSource of the DataGrid.  The User will keep this little application open on their computer kind of like a dashboard.  So, let's say like 10mins later the User wants to see what is new in the SQL table.  I am keeping that initial DataTable in tact and then filling a second DataTable.  

I now have the original DataTable with 500 records.  The second time they hit the button, that second DataTable has 504 records.  I want to then compare the 2 DataTables to find new rows and changed rows.  Once I find the new rows/changes I will show that to the User and make the second DataTable the new "original".  

I have the logic in place that keeps the first DataTable in place and fills a second one. I also have the logic in place to copy the second DataTable to the original making it the new one to compare to.

My stumbling point is how to compare the two DataTables to find the differences!

Thanks for your patience,
jimbo99999
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial