Solved

Comparing datagridviews

Posted on 2009-05-12
4
460 Views
Last Modified: 2013-11-27
Hello Experts

I have an application with 2 datagridviews. Neither is bound to a table.

Can someone tell me the best way to compare the contents? Basically, I have to check for entries in one that do not appear in the other.

I've seen quite a few similar questions suggesting using System.Reflection but they are all firstly in C# and secondly talking about databound datagridviews.

Thanks
0
Comment
Question by:jindalee
  • 2
4 Comments
 
LVL 11

Expert Comment

by:Agarici
Comment Utility
are those 2 grids from your code?
or you want to extract the data from an external application?
0
 
LVL 1

Author Comment

by:jindalee
Comment Utility
My code
0
 
LVL 19

Accepted Solution

by:
Rimvis earned 500 total points
Comment Utility
Something like this?
    Sub CompareDGV(ByVal dgv1 As DataGridView, ByVal dgv2 As DataGridView)
        Dim s1 As String, s2 As String
        Dim i As Integer, j As Integer, k As Integer, l As Integer
        Dim bFound As Boolean 

        'check if same number of columns
        If dgv1.Columns.Count <> dgv2.Columns.Count Then
            Throw New Exception("DataGridViews must have same number of columns")
        End If 
 
        'Check if row in first DGV exists in second DGV
        For i = 0 To dgv1.Rows.Count - 1
            dgv1.Rows(i).Selected = False
            If dgv1.Rows(i).IsNewRow Then Exit For 
            'Get row content
            s1 = ""
            For j = 0 To dgv1.Columns.Count - 1
                s1 += dgv1(j, i).Value.ToString() + "<columndelimiter>"
            Next 
            bFound = False 
            'now examine each row in DVG 2
            For k = 0 To dgv2.Rows.Count - 1
                If dgv2.Rows(k).IsNewRow Then Exit For
                'Get row content
                s2 = ""
                For l = 0 To dgv2.Columns.Count - 1
                    s2 += dgv2(l, k).Value.ToString() + "<columndelimiter>"
                Next 
                'compare rows
                If s1 = s2 Then
                    bFound = True
                    Exit For
                End If 
            Next 
            'if row was not found, select it
            If Not bFound Then dgv1.Rows(i).Selected = True 
        Next 
 
    End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:jindalee
Comment Utility
The code worked very well. However, when I tested with a real data set, over 3000 items in both datagridviews, it was so slowwwwwwwwww, that I'm going to have to rethink how I am to implement. When first asked to do this, I was told there might be 100 or so items not 3000+ !!!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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