【问题标题】:Datagrid row hiding, in wpf vb.netDatagrid行隐藏,在wpf vb.net
【发布时间】:2013-03-28 14:29:36
【问题描述】:

问题是我需要隐藏 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(欢迎任何提示)

提前致谢

【问题讨论】:

    标签: wpf vb.net datagrid row


    【解决方案1】:

    这应该适合你:

    row.Visibility = Windows.Visibility.Collapsed
    


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

    要获得相关的DataGridRow,你必须使用这个函数:

    DataGrid.ItemContainerGenerator.ContainerFromIndex(IndexOfItem)
    

    【讨论】:

    • 不幸的是,row.Visibility 不是可用选项(标记为错误),因为 row 是 DataRowView 的一部分,而不是 DataGridRow:/ 在我的情况下,datagrid 直接绑定到 DataSet(DatasetnameViewSource)。我明天将尝试您提到的 ContainerFromIndex 方法,并留下反馈。不管怎样,谢谢你的回复:)
    • 啊,是的,我已经在 4 月 4 日添加了适合我的解决方案:stackoverflow.com/a/15807601/2220089
    • 我也无法让 containerfromindex 工作,我不记得我遇到的确切问题:/ 也许我用错了,无论如何感谢您的帮助
    【解决方案2】:

    将我的代码更改为:

    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
    

    【讨论】:

      猜你喜欢
      • 2012-01-13
      • 2011-04-24
      • 2011-02-21
      • 1970-01-01
      • 2017-04-05
      • 2020-12-08
      • 1970-01-01
      • 2011-07-18
      • 1970-01-01
      相关资源
      最近更新 更多