troubleshooting Question

VB.NET - Delete duplicated rows no2

Avatar of Wilder1626
Wilder1626Flag for Canada asked on
Visual Basic.NET
9 Comments2 Solutions15 ViewsLast Modified:
This is to continue the first post: 

I'm using below code to remove duplicated rows (all columns) from my DataGridView2.
            For Loop1 As Integer = (DataGridView2.Rows.Count - 2) To 0 Step -1
                For Loop2 As Integer = (DataGridView2.Rows.Count - 2) To Loop1 + 1 Step -1
                    If compare_row(DataGridView2.Rows(Loop1), DataGridView2.Rows(Loop2), DataGridView2.Columns.Count) Then
                    End If
        Catch ex As Exception
            MsgBox("error1 in button click" + vbCrLf + ex.Message)
        End Try

Open in new window

Function compare_row(row1 As DataGridViewRow, row2 As DataGridViewRow, len As Integer) As Boolean

        Dim temp_bool As Boolean = True
        For i = 0 To len - 1
            temp_bool = temp_bool And (row1.Cells(i).Value = row2.Cells(i).Value)
        Return temp_bool

    End Function

Open in new window

I suddenly get the error:
System.InvalidCastException: 'Operator '=' is not defined for string "A" and type 'DBNull'.'

Would you know why and how can i fix this?
The error comes at the Function:

Join our community to see this answer!
Unlock 2 Answers and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros