[英]Datagrid row hiding, in wpf vb.net
問題是我需要根據顯示的監視器隱藏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(歡迎任何提示)
提前致謝
這應該為您工作:
row.Visibility = Windows.Visibility.Collapsed
PS:
在我的范圍內,DataGrid綁定到一個List(Of String)
所以我必須先獲取該行。 因此,在使用DataGrid.Items(i)
您只會得到本身為String
。
要獲取相關的DataGridRow
您必須使用以下函數:
DataGrid.ItemContainerGenerator.ContainerFromIndex(IndexOfItem)
將我的代碼更改為:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.