改变了我的代码:
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
不幸的是,row.Visibility不是一种可行的选择(标记为错误),因为行DataRowView的的一部分,而不是DataGridRow:/ 在我的情况下,datagrid直接绑定到DataSet(DatasetnameViewSource)。 我会尝试你提到的ContainerFromIndex方法,明天和病假留下反馈。 无论如何,感谢您的回复:) – Mitsosp
此处有任何更新? – Basti
啊是的,我已经添加了解决方案,适合我在四月4日: http://stackoverflow.com/a/15807601/2220089 – Mitsosp