Solved

Comparing datagridviews

Posted on 2009-05-12
4
466 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
ID: 24361991
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
ID: 24362125
My code
0
 
LVL 19

Accepted Solution

by:
Rimvis earned 500 total points
ID: 24362322
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
ID: 31623704
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

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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