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

x
?
Solved

VB.net Compare columns in two DataGridViews

Posted on 2013-05-22
3
Medium Priority
?
1,558 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:Murray Brown
[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
  • 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 2000 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:Murray Brown
ID: 39190480
Thanks for the help
0

Featured Post

Technology Partners: 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

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

704 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