Solved

VB.Net - Comparing Two DataTables for Differences

Posted on 2013-10-23
5
4,598 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now