Solved

VB.net Compare columns in two DataGridViews

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

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

630 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