0

问题是我需要根据显示的监视器隐藏 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(欢迎任何提示)

提前致谢

4

2 回答 2

0

这应该适合你:

row.Visibility = Windows.Visibility.Collapsed


PS:
在我的范围内,DataGrid 绑定到 aList(Of String)所以我必须先得到那一行。因此,在使用时, DataGrid.Items(i)您只会得到项目本身,它是一个String.

要获得相关信息DataGridRow,您必须使用此功能:

DataGrid.ItemContainerGenerator.ContainerFromIndex(IndexOfItem)
于 2013-03-28T15:13:53.550 回答
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
于 2013-04-04T09:36:57.800 回答