Solved

VB.net Compare columns in two DataGridViews

Posted on 2013-05-22
3
1,312 Views
Last Modified: 2013-05-23
Hi

I have to write VB.net code to compare two DataGridViews with the same columns.
Each DataGridView has a column called Item_Code and I need to see which item codes in DataGridView1 do not appear in DataGridView2. The initial approach is to loop through the column in DataGridView1 and for each cell then loop through DataGridView2. As there are 1000 rows, this translates to hundreds of thousands if loops. Is there a better way to do this?
0
Comment
Question by:murbro
  • 2
3 Comments
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
Comment Utility
Hi,

Do you have a DataTable that loads the DataGridView? You can utilize LINQ to do the match. If you provide some more info i can help...

Giannis
0
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
Comment Utility
You may check the following example:

        'Define two DataTables
	dim dt1 as datatable = new datatable
	dt1.Columns.Add("Item_Code",Type.GetType("System.Int32"))
	dt1.Columns.Add("Item_Description", Type.GetType("System.String"))
	dim dt2 as datatable = new datatable
	dt2.Columns.Add("Item_Code", Type.GetType("System.Int32"))
	dt2.Columns.Add("Item_Description", Type.GetType("System.String"))
	

        'Load data in the two DataTables
	dim dr as datarow
	dr=dt1.NewRow()
	dr("Item_Code")=1
	dr("Item_Description")="Car"
	dt1.Rows.Add(dr)
	dr=dt1.NewRow()
	dr("Item_Code")=2
	dr("Item_Description")="Bicycle"
	dt1.Rows.Add(dr)
	dr=dt1.NewRow()
	dr("Item_Code")=3
	dr("Item_Description")="Motorcycle"
	dt1.Rows.Add(dr)
	dr=dt2.NewRow()
	dr("Item_Code")=1
	dr("Item_Description")="Car"
	dt2.Rows.Add(dr)
	dr=dt2.NewRow()
	dr("Item_Code")=3
	dr("Item_Description")="Motorcycle"
	dt2.Rows.Add(dr)

        'Define a LINQ query that joins the two Datatables on Item_Code and select only those 
        'rows that exist in first table but not in the second
	dim query = from a in dt1.AsEnumerable() _
				Group join b in dt2.AsEnumerable() _
				on a.Field(of Int32)("Item_Code") equals b.Field(of Int32)("Item_Code") _
				into temp = Group _
				from t in temp.DefaultIfEmpty() _
				where t is nothing _
				select a
	
        'Display the rows in the console (there is only one row in this example.
	for each q in query
		Console.WriteLine(q)
	next

Open in new window


If you need to know more in joining tables, objects or whatever... in LINQ please have a look at my article:
Joining in LINQ

Giannis
0
 

Author Closing Comment

by:murbro
Comment Utility
Thanks for the help
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video discusses moving either the default database or any database to a new volume.
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…

772 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

12 Experts available now in Live!

Get 1:1 Help Now