2013-03-28 82 views
0

问题是,我需要隐藏DataGrid的一些行,根据显示器显示它们。Datagrid行隐藏,在wpf vb.net

这里是我的代码:

For Each row As DataRowView In DataGrid1.Items 
     cellValue = row.Item("Monitor") 
     If cellValue.StartsWith("B") Then 
       //the code i need 
     End If 
Next 

DataGrid1.Items.Remove()DataGrid1.Items.RemoveAt()不能用,因为我的ItemSource在使用中被调用时。

我喜欢改变其知名度隐藏或高度为0

很抱歉,如果这个问题是不正确的格式或很糟糕,这是我的第一个:P(任何提示,欢迎)

在此先感谢

回答

0

改变了我的代码:

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 
0

这应该为你工作:

row.Visibility = Windows.Visibility.Collapsed 


PS:
在我的范围内,DataGrid绑定到List(Of String),所以我必须先获取该行。所以当使用 DataGrid.Items(i)你只会得到这个项目本身,这是一个String

要获得相关DataGridRow你要使用此功能:

DataGrid.ItemContainerGenerator.ContainerFromIndex(IndexOfItem) 
+0

不幸的是,row.Visibility不是一种可行的选择(标记为错误),因为行DataRowView的的一部分,而不是DataGridRow:/ 在我的情况下,datagrid直接绑定到DataSet(DatasetnameViewSource)。 我会尝试你提到的ContainerFromIndex方法,明天和病假留下反馈。 无论如何,感谢您的回复:) – Mitsosp

+0

此处有任何更新? – Basti

+0

啊是的,我已经添加了解决方案,适合我在四月4日: http://stackoverflow.com/a/15807601/2220089 – Mitsosp