Solved

VB.net Compare columns in two DataGridViews

Posted on 2013-05-22
3
1,381 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 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