如何删除重复的数据,包括datagridview vb.net中的原始数据

时间:2019-06-04 02:51:41

标签: c# vb.net duplicates

如何删除datagridview中的重复数据和原始数据?我尝试了不同的代码,但只能删除重复的代码。

Dim i As Integer = 0
While i < numberOfRows

    For ii As Integer = (numberOfRows) To (i + 1) Step -1
        If dtg3.Rows(i).Cells(0).Value.ToString() = 
        dtg3.Rows(ii).Cells(0).Value.ToString() And 
        dtg3.Rows(i).Cells(2).Value.ToString() = 
        dtg3.Rows(ii).Cells(2).Value.ToString() And 
        dtg3.Rows(i).Cells(3).Value.ToString() = 
        dtg3.Rows(ii).Cells(3).Value.ToString() And 
        dtg3.Rows(i).Cells(8).Value.ToString() = 
        dtg3.Rows(ii).Cells(8).Value.ToString() Then
        dtg3.Rows.Remove(dtg3.Rows(ii))

        numberOfRows -= 1
    End If

Next
i += 1
End While

我想要的是,从datagridview中删除重复项和原始数据,希望有人在这里为我提供帮助。

2 个答案:

答案 0 :(得分:0)

你去了

For Each row As DataGridViewRow In DataGridView1.Rows
        For Each nextrow As DataGridViewRow In DataGridView1.Rows

            If row.Index <> nextrow.Index Then
                If row.Cells(0).Value = nextrow.Cells(0).Value Then
                    MsgBox("Duplicate on col 0, index = " & row.Index.ToString)
                End If
                If row.Cells(2).Value = nextrow.Cells(2).Value Then
                    MsgBox("Duplicate on col 2, index = " & row.Index.ToString)
                End If
                If row.Cells(3).Value = nextrow.Cells(3).Value Then
                    MsgBox("Duplicate on col 3, index = " & row.Index.ToString)
                End If
                If row.Cells(8).Value = nextrow.Cells(8).Value Then
                    MsgBox("Duplicate on col 8, index = " & row.Index.ToString)
                End If
            End If

        Next
    Next

这还将检查所有列中是否有重复项,不仅像您的示例中的该行下一样,而且您要递增i和ii,因此您始终只能同时检查2行,而不是将1行与所有其他行进行比较..

答案 1 :(得分:-1)

For Each row As DataGridViewRow In dtg3.Rows
            For Each nextrow As DataGridViewRow In dtg3.Rows
                If row.Index <> nextrow.Index Then
                    If row.Cells(0).Value = nextrow.Cells(0).Value AndAlso 
row.Cells(2).Value = nextrow.Cells(2).Value AndAlso row.Cells(3).Value = 
nextrow.Cells(3).Value AndAlso row.Cells(8).Value = nextrow.Cells(8).Value Then

                        dtg3.Rows.Remove(row)
                        dtg3.Rows.Remove(nextrow)



                    End If
                End If

            Next

        Next