Solved

Comparing datagridviews

Posted on 2009-05-12
4
476 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
[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
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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 brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

707 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