Get datagrid rows

user196625 picture user196625 · Oct 6, 2012 · Viewed 39.1k times · Source

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!

Answer

Rohit Vats picture Rohit Vats · Oct 6, 2012

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);
}