Solved

VB.Net - Comparing Two DataTables for Differences

Posted on 2013-10-23
5
5,549 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

617 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