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()orは使用できません。DataGrid1.Items.RemoveAt()呼び出されたときに ItemSource が使用されているためです。

可視性を非表示にするか、高さを 0 に変更することをお勧めします。

この質問が正しい形式でなかったり、見栄えが悪い場合は申し訳ありませんが、これは私の最初の質問です:P (ヒントは大歓迎です)

前もって感謝します

4

2 に答える 2

0

これはあなたのために働くはずです:

row.Visibility = Windows.Visibility.Collapsed


PS:
私のスコープでは、DataGrid がバインドされてList(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 ループが壊れるためです。したがって、別のループ (モニターごとに 1 つ) で行を削除しています。

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 に答える