从列datagridview vb.net中删除重复数据

时间:2015-11-13 01:04:44

标签: vb.net datagridview datagridviewcolumn

我四处看看并且没有找到答案:/

屏幕截图:http://prntscr.com/9257cg

我希望该条目只出现一次而不是留在那里(“到”列),基本上删除重复项。这是我的更新代码

Private Sub datagridview1Update()

    'Remove rows if there are too many
    If DataGridView1.Rows.Count > 9 Then
        DataGridView1.Rows.RemoveAt(0)
    End If

    DataGridView1.Rows.Add()
    DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(0).Value = ipfrom.ToString 'From Column, size at 125
    DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(1).Value = ipto.ToString  'To Column, size at 125
    DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(2).Value = destinationport.ToString
    DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(3).Value = sourceport.ToString

End Sub

1 个答案:

答案 0 :(得分:0)

首先,您应该遍历DataGridView1的Rows以查看是否已存在与新数据匹配的条目。如果未找到匹配项,请添加新行。此外,您应该只从行数中减去一行以保持正确的排序。

Private Sub datagridview1Update()

    Dim bolFoundMatch As Boolean = False
    Dim intCursor As Integer = 0

    Do Until bolFoundMatch OrElse intCursor = DataGridView1.Rows.Count

        If DataGridView1.Rows(intCursor).Cells(1).Value = ipto.ToString() Then

            bolFoundMatch = True

        End If

        intCursor += 1

    Loop

    If Not bolFoundMatch Then

        'Remove rows if there are too many
        If DataGridView1.Rows.Count > 9 Then
            DataGridView1.Rows.RemoveAt(0)
        End If

        DataGridView1.Rows.Add()
        DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(0).Value = ipfrom.ToString 'From Column, size at 125
        DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(1).Value = ipto.ToString  'To Column, size at 125
        DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(2).Value = destinationport.ToString
        DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(3).Value = sourceport.ToString   

    End If

End Sub