15

How can I get the list of rows in the DataGrid? Not the bound items, but the DataGridRows list.

I need to control the visibility of these rows and it's only possible to control it as a DataGridRow and not as a data object.

Thanks!

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
user196625
  • 199
  • 1
  • 1
  • 6

2 Answers2

36

You can get the row using ItemContainerGenerator. This should work -

for (int i = 0; i < dataGrid.Items.Count; i++)
{
    DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator
                                               .ContainerFromIndex(i);
}
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • 16
    One thing to note here is that when virtualization is on (which it is by default), you will get `null` for rows that weren't created. This means that you won't be able to iterate through all the rows in a single run, unless you set `VirtualizingStackPanel.IsVirtualizing="False"` on your DataGrid - but this may cause your grid to have poor performance. – Adi Lester Oct 06 '12 at 18:42
  • Hello, how can I do that in UWP? – Seaky Lone Aug 11 '19 at 09:24
  • I got value null exception , how can I fix this error? – abdou_dev Nov 24 '20 at 08:37
0

I recommend defining a Style for DataGridRow that will have its Visibility bound to whether it should be displayed or not. Just iterating through the rows won't be enough, as I mentioned in @RV1987's answer.

<DataGrid>
    <DataGrid.Resources>
        <Style TargetType="DataGridRow">
            <Setter Property="Visibility" Value="{Binding ...}" />
        </Style>
    </DataGrid.Resources>
</DataGrid>

EDIT:

What you bind to depends on where you hold the information of whether or not you should display the row. For example, if each data object in your bound collection has a bool ShouldBeDisplayed property, you would have something like this:

<DataGrid>
    <DataGrid.Resources>
        <BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter" />

        <Style TargetType="DataGridRow">
            <Setter Property="Visibility" Value="{Binding Path=ShouldBeDisplayed, Converter={StaticResource booleanToVisibilityConverter}}" />
        </Style>
    </DataGrid.Resources>
</DataGrid>
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
  • Binding of what?This is not a uielemnt...can u be more specific? – user196625 Oct 07 '12 at 13:05
  • Does not work sorry... I Add this property: I try this: and add this property to the object: public Visibility ShouldBeDisplayed { get; set; } Whats wrong?thank u! – user196625 Oct 07 '12 at 16:10
  • @user196625 Did you actually set the value of the property for each item? – Adi Lester Oct 07 '12 at 17:17
  • yes..I set it by clicking a button..maybe it is not set immediatlly? There is a binding i check it but in running time when I change it nothing happened. – user196625 Oct 07 '12 at 17:31
  • If it's not set when the control is bound, you'll also have to implement `INotifyPropertyChanged` for the objects. – Adi Lester Oct 07 '12 at 17:56