Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Comparing datagridviews

Posted on 2009-05-12
4
Medium Priority
?
478 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 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

610 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