Hello guys.

Im using visual studio 2005 and access 2007 as database. I have already connect the database which have two table with two gridview in a web form (asp.net). Now i want to compared the data in the two gridview, and those who are matching should be displayed in a pop up Text box. All this should be done by clicking on a single button.

The following is the codes which i have tried and it does not work. I dont know where the problem. The following codes are for those who does not matched, which will be display. please answer should be in vb.net not C#


These codes are found inside my button "compare"

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim gv1RowCnt, gv2RowCnt As Integer
        Dim diff As Boolean = False
        gv1RowCnt = GridView1.Rows.Count
        gv2RowCnt = GridView2.Rows.Count


        Dim rw1, rw2 As GridViewRow
        If gv1RowCnt > gv2RowCnt Then
            For Each rw2 In GridView2.Rows
                For Each rw1 In GridView1.Rows

                    If rw1.Cells(0).Text <> rw2.Cells(0).Text Then
                        diff = True
                    End If

                Next
            Next
        ElseIf gv2RowCnt >= gv1RowCnt Then
            For Each rw1 In GridView1.Rows
                For Each rw2 In GridView2.Rows

                    If rw1.Cells(0).Text <> rw2.Cells(0).Text Then
                        diff = True
                    End If

                Next
            Next
        Else


        End If
        Dim oAry1 As New ArrayList(New String() {})
        Dim oAry2 As New ArrayList(New String() {})
        Dim oAry3 As New ArrayList()
        For Each i As String In oAry1
            If Not oAry2.Contains(i) Then oAry3.Add(i)
        Next
        For Each i As String In oAry2
            If Not oAry1.Contains(i) Then oAry3.Add(i)
        Next
        oAry3.Sort()
        MsgBox(Join(oAry3.ToArray, ","))
    End Sub
End Class

Recommended Answers

All 4 Replies

You don't need to have two separate loops depending on the relative number of rows. You can do it in one loop as

Private Sub btnCompare_Click(sender As System.Object, e As System.EventArgs) Handles btnCompare.Click

        For Each rw1 As DataGridViewRow In DataGridView1.Rows

            For Each rw2 As DataGridViewRow In DataGridView2.Rows
                If rw1.Cells(0).Value = rw2.Cells(0).Value Then
                    If Not rw1.Cells(0).Value Is Nothing Then
                        MsgBox(rw1.Cells(0).Value & "  found in grid 2")
                        Exit For
                    End If
                End If
            Next

        Next

    End Sub

I'm using Visual Studio 2010 so I have to use DataGridView instead of GridView but the method should be the same. The test for

If Not rw1.Cells(0).Value Is Nothing Then

is to prevent a match on the empty last row in the two grids.

Hello Reverend Jim

I will test your code and will let you know soon. What i have to do just to copy your code and paste it, into the button compare. And change the 'datagridview' to only 'gridview'. And the result should be that, it will give me only data which are matching between these two gridview. Thank for your help friend..

To Reverend Jim

Ive test it. I got the following error for rw1.Cells(0).Value. Its says 'Value' is not a member of System.Web.UI.WebControls.TableCell". Maybe i have to declare some other classes? Any idea from ur part.

I don't have access to a GridView class in VB 2010. What I suggest is to run the app in the IDE and when it stops on the line with the error, put a watch on rw1 and look for the property containing the cell value. Then change your code to use that property instead of "Value".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.