Articles in this section
Category / Section

How to navigate to the error cells in WPF DataGrid (SfDataGrid) via button click?

1 min read

WPF DataGrid (SfDataGrid) does not have support to navigate through the error cells specifically. You can navigate to the error cells using the CurrentRowColumnIndex and GridCell.HasError properties through button click.


private void OnFindNextClicked(object obj)
      var datagrid = obj as SfDataGrid;
      var visualContainer = datagrid.GetVisualContainer();
      var moveNextRow = true;
      var currentRowColumnIndex = datagrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex;
      //if there is no selection maintained in grid, currentRowColumnIndex will be -1. In that case we need to navigate to the first error cell.
      if (currentRowColumnIndex.RowIndex < 0)
           MoveToErrorCell(datagrid, true, ref moveNextRow);
      var rowIndex = currentRowColumnIndex.RowIndex;
      var columnIndex = currentRowColumnIndex.ColumnIndex;
      while (moveNextRow && rowIndex >= datagrid.GetFirstDataRowIndex() && rowIndex <= datagrid.GetLastDataRowIndex())
          var gridCellCollections = GetGridCellCollection(datagrid, new RowColumnIndex(rowIndex,columnIndex));
          if (gridCellCollections != null)
               foreach (var gridCellItem in gridCellCollections)
                    var gridCell = gridCellItem as GridCell;
                    if (gridCell != null && gridCell.HasError && (gridCell.ColumnBase.ColumnIndex > columnIndex || currentRowColumnIndex.RowIndex != rowIndex))
                         datagrid.SelectionController.MoveCurrentCell(new RowColumnIndex(rowIndex, gridCell.ColumnBase.ColumnIndex));
                         datagrid.ScrollInView(datagrid.SelectionController. CurrentCellManager.CurrentRowColumnIndex);                         
                         moveNextRow = false;
           if (rowIndex <= datagrid.GetLastDataRowIndex())
               datagrid.SelectionController.MoveCurrentCell(new RowColumnIndex(rowIndex, datagrid.GetFirstColumnIndex()));
/// <summary>
/// To get the grid cell collection of particular row
/// </summary>
private UIElementCollection GetGridCellCollection(SfDataGrid datagrid, RowColumnIndex currentRowColumnIndex)
     var currentRow = datagrid.RowGenerator.Items.FirstOrDefault(row => row.RowIndex == currentRowColumnIndex.RowIndex);
     var rowControl = currentRow.Element as VirtualizingCellsControl;
     var orientedCellsPanel = rowControl.Content as OrientedCellsPanel;
     return orientedCellsPanel.Children;

View sample in GitHub.

Did you find this information helpful?
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied