Search in MAUI DataGrid (SfDataGrid)

28 Aug 20255 minutes to read

The SfDataGrid control allows you to search the data displayed within it. You can search the data using the SearchController.Search method.

this.dataGrid.SearchController.Search(entry.Text);

DataGrid with Search Panel

Filtering

The filtering can be enabled for the search results by setting the SearchController.AllowFiltering property to true.

this.dataGrid.SearchController.AllowFiltering = true;
this.dataGrid.SearchController.Search(entry.Text);

Enabling Filter based on Search in DataGrid

You can search the data with the case-sensitivity by setting SearchController.AllowCaseSensitive property.

this.dataGrid.SearchController.AllowCaseSensitive = true;

Search with pattern matching

You can customize the type of search for the SearchController by using the SearchType property. The available search types are: StartsWith, Contains and EndsWith. The below code snippet demonstrates how to set the search type as StartsWith.

this.dataGrid.SearchController.SearchType = DataGridSearchType.StartsWith;

Customize the appearance of highlight text

You can apply the style for a searched text color, background color and search highlighted text color, background color by using SearchTextColor, SearchTextBackground, SearchHighlightTextColor, SearchHighlightTextBackground in SfDataGrid.DefaultStyle.

<ContentPage.Content>
    <syncfusion:SfDataGrid ItemsSource="{Binding OrderInfoCollection}">
        <syncfusion:SfDataGrid.DefaultStyle>
            <syncfusion:DataGridStyle SearchTextColor="Black" 
                                    SearchTextBackground="LightBlue" 
                                    SearchHighlightTextColor="Black" 
                                    SearchHighlightTextBackground="LightGreen" />
        </syncfusion:SfDataGrid.DefaultStyle>
    </syncfusion:SfDataGrid>
</ContentPage.Content>

The SfDataGrid allows navigation between the search results programmatically using the SearchController.FindNext and SearchController.FindPrevious methods.

this.dataGrid.SearchController.FindNext("SearchText");
this.dataGrid.SearchController.FindPrevious("SearchText");

Navigated Search Text in DataGrid

The search can be cleared by using the SearchController.ClearSearch method.

this.dataGrid.SearchController.ClearSearch();

Search customization

The SfDataGrid processes search operations in the DataGridSearchController class. You can change the default search behaviors by creating a custom class that overrides the DataGridSearchController class and setting it to the SfDataGrid.SearchController property.

this.dataGrid.SearchController = new CustomDataGridSearchController (this.datagrid);
public class CustomDataGridSearchController  : DataGridSearchController
{
    public CustomDataGridSearchController(SfDataGrid datagrid)
        : base(datagrid)
    {
    }
}

Search only a specific column

You can search only a specific column by overriding the SearchCell method of the DataGridSearchController. In the SearchCell method, you can perform a search for the column you want to apply based on the MappingName.

In the code below, all columns except the CustomerID column are excluded from the search.

public class CustomDataGridSearchController : DataGridSearchController
{
    public CustomDataGridSearchController(SfDataGrid datagrid)
        : base(datagrid)
    {
    }
    protected override bool SearchCell(DataColumnBase column, object record, bool ApplySearchHighlightBrush)
    {

        if (column.DataGridColumn.MappingName == "CustomerID")
            return base.SearchCell(column, record, ApplySearchHighlightBrush);
        return false;
    }
}

DataGrid displays Search Text only in Selected Column

Obtain the search records

You can get the records by using the GetSearchRecords method of the DataGridSearchController. This method returns a list of search records. The below code snippet demonstrates how to get the search records.

var records = this.dataGrid.SearchController.GetSearchRecords();