troubleshooting Question

VB.NET - Delete duplicated rows no2

Avatar of Wilder1626
Wilder1626Flag for Canada asked on
Visual Basic.NET
9 Comments2 Solutions15 ViewsLast Modified:
Hi,
This is to continue the first post: https://www.experts-exchange.com/questions/29219082/VB-NET-Delete-duplicated-rows.html 

I'm using below code to remove duplicated rows (all columns) from my DataGridView2.
 Try
            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
                        DataGridView2.Rows.RemoveAt(Loop2)
                    End If
                Next
            Next
        Catch ex As Exception
            MsgBox("error1 in button click" + vbCrLf + ex.Message)
        End Try

Open in new window

Function
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)
        Next
        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:

Thanks
ASKER CERTIFIED SOLUTION
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