Avatar of Wilder1626
Wilder1626
Flag for Canada

asked on 

VB.NET - Delete duplicated rows no2

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
Visual Basic.NET

Avatar of undefined
Last Comment
Wilder1626

8/22/2022 - Mon