Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.Net - Comparing Two DataTables for Differences

Posted on 2013-10-23
5
Medium Priority
?
5,866 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

670 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