Is it possible to iterate over DataGrid rows?

Jan 1, 2009 at 4:59 PM
Is it possible to iterate over the rows over a datagrid programmatically?

(I need it for unit tests of a data grid.  Both for testing the displayed contents, and for actually selecting a row)
Jan 7, 2009 at 1:29 PM
You can use the GetRow helper function that is described on this thread,
Jan 7, 2009 at 7:30 PM
Synchronicity!  I've started using both the GetRow() and GetCell() helper functions described in the thread you're referring to... today.

But I am getting some strange NullReferenceExceptions, that i didn't get with the old approach (selecting everything and using the SelectedCells property, to find the cell values).

Here's the stack trace:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Controls.VirtualizingStackPanel.InsertContainer(Int32 childIndex, UIElement container, Boolean isRecycled)
at System.Windows.Controls.VirtualizingStackPanel.AddContainerFromGenerator(Int32 childIndex, UIElement child, Boolean newlyRealized)
at System.Windows.Controls.VirtualizingStackPanel.BringIndexIntoView(Int32 index)
at Microsoft.Windows.Controls.Primitives.DataGridRowsPresenter.InternalBringIndexIntoView(Int32 index) in DataGridRowsPresenter.cs: line 48
at Microsoft.Windows.Controls.DataGrid.ScrollRowIntoView(Object item) in DataGrid.cs: line 1617
at Microsoft.Windows.Controls.DataGrid.ScrollIntoView(Object item) in DataGrid.cs: line 1506
at Common.DataGridExtensions.GetRow(DataGrid dataGrid, Int32 index) in DataGridExtensions.cs: line 51
at StandardDevices.Test.SorterConfigurationPanelTest.GetSortDefinitionFromGridRow(DataGrid grid, Int32 rowIndex) in SorterConfigurationPanelTest.cs: line 415
at StandardDevices.Test.SorterConfigurationPanelTest.<TestUpAndDownButtons>b__12() in SorterConfigurationPanelTest.cs: line 218
at TestUtilities.CrossThreadTestRunner.<>c__DisplayClass1.<RunSingleThreadedApartment>b__0() in CrossThreadTestRunner.cs: line 34
at TestUtilities.CrossThreadTestRunner.ThrowExceptionPreservingStack(Exception exception) in CrossThreadTestRunner.cs: line 62
at TestUtilities.CrossThreadTestRunner.RunSingleThreadedApartment(ThreadStart userDelegate) in CrossThreadTestRunner.cs: line 51
at StandardDevices.Test.SorterConfigurationPanelTest.TestUpAndDownButtons() in SorterConfigurationPanelTest.cs: line 163

What I've done here is to manipulate the model collection in the click handlers for the up and down buttons, and then call UpdateTarget() on the binding expression.

But this is perhaps not the recommended way to move rows (and add and delete rows) in a WPF DataGrid...?
Jun 8, 2009 at 11:33 AM

Please call DataGrid.UpdateLayout() before calling DataGrid.ScrollIntoView(...) inside the GetRow() and GetCell()

Jul 1, 2010 at 1:57 PM

// to interate through all the rows , all the cells and alert the cell value


 var row = GetDataGridRows(DataGrid_Standard);
                /// go through each row in the datagrid
                foreach (Microsoft.Windows.Controls.DataGridRow r in row)
                    DataRowView rv = (DataRowView)r.Item;
                    foreach (DataGridColumn column in DataGrid_Standard.Columns)
                        if (column.GetCellContent(r) is TextBlock)
                            TextBlock cellContent = column.GetCellContent(r) as TextBlock;
                        else if (column.GetCellContent(r) is CheckBox)
                            CheckBox chk = column.GetCellContent(r) as CheckBox;
                            MessageBox .Show (chk.IsChecked.ToString());

public IEnumerable<Microsoft.Windows.Controls.DataGridRow> GetDataGridRows(Microsoft.Windows.Controls.DataGrid grid)

            var itemsSource = grid.ItemsSource as IEnumerable;

            if (null == itemsSource) yield return null;

            foreach (var item in itemsSource)

                var row = grid.ItemContainerGenerator.ContainerFromItem(item) as Microsoft.Windows.Controls.DataGridRow;

                if (null != row) yield return row;



Jul 31, 2013 at 6:43 AM
I am getting following error
The type or namespace name 'Controls' does not exist in the namespace 'Microsoft.Windows' (are you missing an assembly reference?)