Solved

VB.Net - Comparing Two DataTables for Differences

Posted on 2013-10-23
5
4,950 Views
Last Modified: 2013-11-11
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
0
Comment
Question by:Jimbo99999
  • 3
  • 2
5 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39594439
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
0
 

Author Comment

by:Jimbo99999
ID: 39595469
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
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39597762
Are the changes saved between original fill and user clicking the button?
0
 

Author Comment

by:Jimbo99999
ID: 39598311
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
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39619347
I created a sample project and the result is attached. Do pay attention to what is being shown in Immediate Window at lower left corner.

Screenshot
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question