Wilder1626
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.
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
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
FunctionFunction 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
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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
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 suggest you fully read my previous full solution and try to understand fully what I said, and then add something of my code back.
ASKER
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?
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
Using the row.IsNewRow check, is easier and makes your code more readable.
ASKER
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:
Will continue to look at it.
So based on how i understand it, i've updated the code to below:
If row1.IsNewRow = False AndAlso row2.IsNewRow = False Then
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
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks. Yes, you are right, this is working.
ASKER
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 <> ""?