Datagrid行隐藏,在wpf vb.net中

时间:2013-03-28 14:29:36

标签: wpf vb.net datagrid row

问题是我需要根据显示的监视器隐藏DataGrid的某些行。

这是我的代码:

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(欢迎任何提示)

提前致谢

2 个答案:

答案 0 :(得分:0)

这应该适合你:

row.Visibility = Windows.Visibility.Collapsed


P.S:
在我的范围内,DataGrid绑定到List(Of String)所以我必须先获得该行。所以在使用时 DataGrid.Items(i)您只能获得String项目本身。

要获取相关的DataGridRow,您必须使用此功能:

DataGrid.ItemContainerGenerator.ContainerFromIndex(IndexOfItem)

答案 1 :(得分: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