We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WPF |
Control: SfDataGrid

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.

C#

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;
                         return;
                    }
                }
           }
 
           rowIndex++;
           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.

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile