Articles in this section
Category / Section

How to perform the incremental search in WPF DataGrid (SfDataGrid)?

2 mins read

You can increase the searching operation in WPF DataGrid (SfDataGrid) by using SearchHelper class. The SearchHelper class is used to perform the search operation in the SfDataGrid. The SearchBrush property can be used to highlight the search text of grid with default color(yellow). This feature can be added to the SfDataGrid by including the namespace as Syncfusion.UI.Xaml.Grid.Helpers.

 

You can use the Search method of SearchHelper class to search the record. From the attached sample, to enable the search panel, select the row in the grid and press Ctrl+F key in the keyboard.

 

this.dataGrid.SearchHelper.Search(TextBox.Text);

 

Below code illustrates on to find the previous and next matched records based on the given text by using FindNext and FindPrevious method. You can use the navigation arrow in the search panel to move the focus to that searched record from the attached sample.

 

// move the current cell and search the next records in the SfDataGrid
this.dataGrid.SearchHelper.FindNext(TextBox.Text);
this.dataGrid.SelectionController.MoveCurrentCell(this.dataGrid.SearchHelper.CurrentRowColumnIndex);
 
// move the current cell and search the previous records in the SfDataGrid
this.dataGrid.SearchHelper.FindPrevious(TextBox.Text);
this.dataGrid.SelectionController.MoveCurrentCell(this.dataGrid.SearchHelper.CurrentRowColumnIndex);

 

Below code will be used to search only the selected columns by overriding SearchCell method of SearchHelper.

 

DataGrid.SearchHelper = new SearchHelperExt(DataGrid);
DataGrid.SearchHelper.Search(SearchTextBox.Text);
 
public class SearchHelperExt : SearchHelper
{
    public SearchHelperExt(SfDataGrid datagrid)
        : base(datagrid)
    {
    }
 
    protected override bool SearchCell(DataColumnBase column, object record, bool ApplySearchHighlightBrush)
    {
                 // to get the columnIndex of selected row of the SfDataGrid
        var colIndex = DataGrid.SelectionController.CurrentCellManager.CurrentCell.ColumnIndex;
        // to get the mapping name of based on the column index 
        var mapName = DataGrid.Columns[colIndex].MappingName;
        // based on the mapping name, the particular searching will be enabled other columns the searching will be excluded. 
        if (column.GridColumn.MappingName == mapName)
            return base.SearchCell(column, record, ApplySearchHighlightBrush);
        return false;
    }
}

 

 

Refer the below example which demonstrates an incremental search with the SfDataGrid.

 

Increase the search operation in WPF DataGrid

View sample in GitHub.

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