将我的代码更改为:
Dim numOfRows As Integer
Dim listA As New List(Of Integer)
Dim listB As New List(Of Integer)
Dim deleted As Integer = 0
For Each row As DataRowView In DataGrid1.Items
numOfRows += 1 'Adding up to total number of rows'
If row.Item("Monitor").ToString.StartsWith("A") Then
listA.Add(numOfRows - 1) 'Adding rows indexes to a list'
ElseIf row.Item("Monitor").ToString.StartsWith("B") Then
listB.Add(numOfRows - 1) 'Adding rows indexes to a list'
End If
Next
我没有使用的原因row.Delete()
是如果我在运行中更改其项目源,则 For Each 循环会中断。因此,我正在删除另一个循环上的行(每个监视器一个):
Dim rowA As DataRowView
For Each litem As Integer In listA
litem -= deleted
'The indexes on the list were added in a sorted order'
'ex. {4,7,14,28,39} . So if i delete the fourth row'
'all of the rows that remain will have their index'
'decreased by one,so by using an integer that represents'
'the number of the deleted rows, i can always delete the'
'correct "next" row'
rowA = DataGrid1.Items.Item(litem)
rowA.Delete()
deleted += 1
Next