Solved

VB.net Compare columns in two DataGridViews

Posted on 2013-05-22
3
1,355 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
ID: 39190020
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
ID: 39190157
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
ID: 39190480
Thanks for the help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WPF - Tooltips for ComboBox items 5 38
Convert to Hour/minute textbox and compare 3 53
Unable  to create new object 9 41
More on Time zones in vb 2010 12 37
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…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

18 Experts available now in Live!

Get 1:1 Help Now