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

Removing duplicate data out of column datagridview vb.net

我环顾四周,没有找到答案:/

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

我希望该条目只显示一次而不是停留在那里("to" 列),基本上删除重复项。这是我的更新代码

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

首先,您应该遍历 DataGridView1 的行,看看是否已经有匹配新数据的条目。如果未找到匹配项,则添加新行。此外,您应该只从行数中减去一个以保持正确的顺序。

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