从BindingSource中删除特定行

时间:2018-04-25 12:32:50

标签: .net vb.net

我需要从Environment.Username删除包含BindingSource的所有行。

有人能帮帮我吗?谁能帮助我?我正在运行下面的代码,但是一直有索引错误(但我知道记录存在):

If BS.Count > 0 Then
    Dim row As Integer = 0
    For Each BSRow As DataRowView In BS
        If DirectCast(BS(row), DataRowView)("NTUsername").ToString = Environment.UserName Then
            .RemoveAt(row)
        End If
        row += 1
    Next
End If

编辑:根据评论

If BS.Count > 0 Then
    For row As Integer = BS.Count To 0
        If DirectCast(BS(row), DataRowView)("NTUsername").ToString = Environment.UserName Then
            .RemoveAt(row)
        End If
    Next
End If

1 个答案:

答案 0 :(得分:0)

For row As Integer = BS.Count -1 To 0 Step -1 

.Net中的大多数内容都是零基础,因此计数比索引多一个。