[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 479
  • Last Modified:

Comparing datagridviews

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
jindalee
Asked:
jindalee
  • 2
1 Solution
 
AgariciCommented:
are those 2 grids from your code?
or you want to extract the data from an external application?
0
 
jindaleeAuthor Commented:
My code
0
 
RimvisCommented:
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
 
jindaleeAuthor Commented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now