Link to home
Create AccountLog in
Avatar of Wilder1626
Wilder1626Flag 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'.'
User generated image
Would you know why and how can i fix this?
The error comes at the Function:
User generated image
Thanks
ASKER CERTIFIED SOLUTION
Avatar of Kimputer
Kimputer

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Wilder1626

ASKER

Debug by hovering over the i value, as well as trying to get the index or row 1 and row 2.

If I go over the i value, i see "0".
Then, if i go over row1, i have "A", and for row2, i have "".
So i'm assuming that the empty row at the end causes the problem by saying, Is not defined for String "A" and type 'DBNull'

Just not sure how to fix that one. Do i need to put somehting like:  if i <> ""?


I was able to make it work now:
  Function compare_row(row1 As DataGridViewRow, row2 As DataGridViewRow, len As Integer) As Boolean
        Try
            Dim temp_bool As Boolean = True
            For i = 0 To len - 1
                If Not IsDBNull(row1.Cells(i).Value) AndAlso Not IsDBNull(row2.Cells(i).Value) Then
                    temp_bool = temp_bool And (row1.Cells(i).Value = row2.Cells(i).Value)
                End If
            Next
            Return temp_bool

        Catch ex As Exception
            MsgBox("error1 in button click" + vbCrLf + ex.Message)
        End Try


    End Function

Open in new window


Avatar of Kimputer
Kimputer

I only now notice, you forgot to include my previous solution. I explained about that last row, and TWO solutions.
I suggest you fully read my previous full solution and try to understand fully what I said, and then add something of my code back.
I did included your previous solution at the beginning of this post. No?

I'm really sorry. I'm really trying to learn here. This is very new to me in VB.Net.
Isn't that doing it to fix the problem?
 If Not IsDBNull(row1.Cells(i).Value) AndAlso Not IsDBNull(row2.Cells(i).Value) Then
 

Open in new window

Using the row.IsNewRow  check, is easier and makes your code more readable.
Thank you for your patience with me. I really appreciate it.
So based on how i understand it, i've updated the code to below:
If row1.IsNewRow = False AndAlso row2.IsNewRow = False Then

Open in new window


Function compare_row(row1 As DataGridViewRow, row2 As DataGridViewRow, len As Integer) As Boolean
        Try
            Dim temp_bool As Boolean = True
            For i = 0 To len - 1
                If row1.IsNewRow = False AndAlso row2.IsNewRow = False Then
                    temp_bool = temp_bool And (row1.Cells(i).Value = row2.Cells(i).Value)
                End If
            Next
            Return temp_bool

        Catch ex As Exception
            MsgBox("error1 in button click" + vbCrLf + ex.Message)
        End Try


    End Function

Open in new window

If i did understand it correctly, i'm still having : System.InvalidCastException: 'Operator '=' is not defined for string "A" and type 'DBNull'.' 

Will continue to look at it.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Thanks. Yes, you are right,  this is working.