首页 > 解决方案 > 如何删除重复数据,包括datagridview vb.net中的原始数据

问题描述

如何删除 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 中删除重复数据和原始数据,希望有人在这里帮助我。

标签: c#vb.netduplicates

解决方案


你去吧

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 行与所有其他行进行比较..


推荐阅读