在两个 DGV 中查找重复项
Find duplicates in two DGVs
我在这上面花了很多时间,但一无所获。我有两个 DGV 并尝试比较两列的相同值,如果 DGV2 中的值不在 DGV1 中,则该值应转到 DGV3。这里的问题是合乎逻辑的,但是在这个错误的解决方案中主演太久让我无法进一步前进。
在我看来,应该在 IF
之前搜索整个 DGV1 以查找 DGV2 中的第一个值,并搜索 DGV2 中的所有其他值。我不知道我真的需要帮助。
谢谢。
Dim row As String() = New String() {"", "", "", ""}
Dim x As Integer = 0
For A = 0 To DGV2.RowCount - 1
For B = 0 To DGV1.RowCount - 1
If GetTextOrEmpty(DGV1.Rows(A).Cells(4).Value) = GetTextOrEmpty(DGV2.Rows(B).Cells(1).Value) Then
Else
'PROBLEM : This is going to add row WITH SAME VALUES everytime it's <>
DGV3.Rows.Add(row)
DGV3.Rows(x).Cells(0).Value = DGV2.Rows(B).Cells(0).Value
DGV3.Rows(x).Cells(1).Value = DGV2.Rows(B).Cells(2).Value
DGV3.Rows(x).Cells(2).Value = DGV2.Rows(B).Cells(1).Value
DGV3.Rows(x).Cells(3).Value = DGV2.Rows(B).Cells(3).Value
x = x + 1
End If
Next
Next
编辑:没关系。问题解决了:
Dim row As String() = New String() {"", "", "", ""}
Dim x As Integer = 0
For A = DGV2.RowCount - 1 To 0 Step -1
For B = 0 To DGV1.RowCount - 1
If GetTextOrEmpty(DGV1.Rows(B).Cells(4).Value) = GetTextOrEmpty(DGV2.Rows(A).Cells(1).Value) Then
DGV2.Rows.Remove(DGV2.Rows(A))
End If
Next
Next
For i = 0 To DGV2.RowCount - 1
DGV3.Rows.Add(row)
DGV3.Rows(x).Cells(0).Value = DGV2.Rows(i).Cells(0).Value
DGV3.Rows(x).Cells(1).Value = DGV2.Rows(i).Cells(2).Value
DGV3.Rows(x).Cells(2).Value = DGV2.Rows(i).Cells(1).Value
DGV3.Rows(x).Cells(3).Value = DGV2.Rows(i).Cells(3).Value
x = x + 1
Next
使用 HashSet
会快一点:
Dim values = New HashSet(Of String)(From i In Enumerable.Range(0, DGV1.RowCount)
Select Convert.ToString(DGV1(4, i).Value))
For Each r As DataGridViewRow In DGV2.Rows
If Not values.Contains(Convert.ToString(r.Cells(0).Value)) Then
DGV3.Rows.Add(r.Cells(0).Value, r.Cells(1).Value, r.Cells(2).Value, r.Cells(3).Value)
End If
Next
我在这上面花了很多时间,但一无所获。我有两个 DGV 并尝试比较两列的相同值,如果 DGV2 中的值不在 DGV1 中,则该值应转到 DGV3。这里的问题是合乎逻辑的,但是在这个错误的解决方案中主演太久让我无法进一步前进。
在我看来,应该在 IF
之前搜索整个 DGV1 以查找 DGV2 中的第一个值,并搜索 DGV2 中的所有其他值。我不知道我真的需要帮助。
谢谢。
Dim row As String() = New String() {"", "", "", ""}
Dim x As Integer = 0
For A = 0 To DGV2.RowCount - 1
For B = 0 To DGV1.RowCount - 1
If GetTextOrEmpty(DGV1.Rows(A).Cells(4).Value) = GetTextOrEmpty(DGV2.Rows(B).Cells(1).Value) Then
Else
'PROBLEM : This is going to add row WITH SAME VALUES everytime it's <>
DGV3.Rows.Add(row)
DGV3.Rows(x).Cells(0).Value = DGV2.Rows(B).Cells(0).Value
DGV3.Rows(x).Cells(1).Value = DGV2.Rows(B).Cells(2).Value
DGV3.Rows(x).Cells(2).Value = DGV2.Rows(B).Cells(1).Value
DGV3.Rows(x).Cells(3).Value = DGV2.Rows(B).Cells(3).Value
x = x + 1
End If
Next
Next
编辑:没关系。问题解决了:
Dim row As String() = New String() {"", "", "", ""}
Dim x As Integer = 0
For A = DGV2.RowCount - 1 To 0 Step -1
For B = 0 To DGV1.RowCount - 1
If GetTextOrEmpty(DGV1.Rows(B).Cells(4).Value) = GetTextOrEmpty(DGV2.Rows(A).Cells(1).Value) Then
DGV2.Rows.Remove(DGV2.Rows(A))
End If
Next
Next
For i = 0 To DGV2.RowCount - 1
DGV3.Rows.Add(row)
DGV3.Rows(x).Cells(0).Value = DGV2.Rows(i).Cells(0).Value
DGV3.Rows(x).Cells(1).Value = DGV2.Rows(i).Cells(2).Value
DGV3.Rows(x).Cells(2).Value = DGV2.Rows(i).Cells(1).Value
DGV3.Rows(x).Cells(3).Value = DGV2.Rows(i).Cells(3).Value
x = x + 1
Next
使用 HashSet
会快一点:
Dim values = New HashSet(Of String)(From i In Enumerable.Range(0, DGV1.RowCount)
Select Convert.ToString(DGV1(4, i).Value))
For Each r As DataGridViewRow In DGV2.Rows
If Not values.Contains(Convert.ToString(r.Cells(0).Value)) Then
DGV3.Rows.Add(r.Cells(0).Value, r.Cells(1).Value, r.Cells(2).Value, r.Cells(3).Value)
End If
Next