# Toolbar SearchBox

In addition to [Grid filtering](https://www.telerik.com/blazor-ui/documentation/components/grid/filter/overview), you can enhance functionality by adding a SearchBox to the [Grid Toolbar](https://www.telerik.com/blazor-ui/documentation/components/grid/toolbar). This search box filters multiple Grid columns simultaneously.

Users type their query, and the Grid performs a case-insensitive Contains search on all visible string columns. To customize the filter delay and searchable columns, see the [Customize the SearchBox section](https://www.telerik.com/blazor-ui/documentation/components/grid/filter/searchbox#customize-the-searchbox).

The SearchBox operates independently of Grid filtering, respecting existing filters and adding extra criteria to refine search results.

## Basic Usage

To enable the built-in Grid SearchBox, use one of the following options:

* Add a `<GridToolBarSearchBoxTool>` tag to the [Grid ToolBar](https://www.telerik.com/blazor-ui/documentation/components/grid/toolbar). This approach is suitable if you are not using a Grid ToolBar yet, or if you are using only built-in ToolBar tools.
  ```RAZOR.skip-repl
  <TelerikGrid>
      <GridToolBar>
          <GridToolBarSearchBoxTool />
      </GridToolBar>
  </TelerikGrid>
  ```

* Add a `<GridSearchBox>` tag to the [Grid ToolBar Template](https://www.telerik.com/blazor-ui/documentation/components/grid/toolbar#custom-toolbar-configuration). This approach is suitable if you have or need custom Grid ToolBar content.
  ```RAZOR.skip-repl
  <TelerikGrid>
      <GridToolBarTemplate>
          <GridSearchBox />
      </GridToolBarTemplate>
  </TelerikGrid>
  ```

The following example shows the first option in action, while the [customization demo](https://www.telerik.com/blazor-ui/documentation/components/grid/filter/searchbox#customize-the-searchbox) below uses a `GridToolBarTemplate`.

> caption Grid SearchBox

<demo metaUrl="client/grid/filter-searchbox/" height="550"></demo>

## Search From Code

You can set or remove the search filters programmatically through the `SearchFilter` property of the [Grid state](https://www.telerik.com/blazor-ui/documentation/components/grid/state).

> caption Set and clear the Grid SearchBox filter programmatically

```RAZOR
@using Telerik.DataSource

<TelerikGrid @ref="@GridRef"
             Data="@GridData"
             Pageable="true"
             Sortable="true">
    <GridToolBar>
        <GridToolBarCustomTool>
            <TelerikButton Icon="@SvgIcon.Search"
                        ThemeColor="@ThemeConstants.Button.ThemeColor.Primary"
                        OnClick="@OnSearchButtonClick">Search Programmatically</TelerikButton>
            <TelerikButton Icon="@SvgIcon.X"
                        OnClick="@OnClearButtonClick">Clear Search</TelerikButton>
        </GridToolBarCustomTool>
        <GridToolBarSearchBoxTool />
    </GridToolBar>
    <GridColumns>
        <GridColumn Field="@nameof(GridModel.Name)" />
        <GridColumn Field="@nameof(GridModel.Description)" />
    </GridColumns>
</TelerikGrid>

@code {
    private TelerikGrid<GridModel>? GridRef { get; set; }

    private List<GridModel> GridData { get; set; } = new();

    private async Task OnSearchButtonClick()
    {
        if (GridRef is null)
        {
            return;
        }

        GridState<GridModel> gridState = GridRef.GetState();

        string searchString = $"{(char)Random.Shared.Next(97, 123)}{(char)Random.Shared.Next(97, 123)}";

        CompositeFilterDescriptor cfd = new();

        cfd.LogicalOperator = FilterCompositionLogicalOperator.Or;
        cfd.FilterDescriptors = new FilterDescriptorCollection();

        // Add one FilterDesccriptor for each string column
        cfd.FilterDescriptors.Add(new FilterDescriptor()
        {
            Member = nameof(GridModel.Name),
            MemberType = typeof(string),
            Operator = FilterOperator.Contains,
            Value = searchString
        });
        cfd.FilterDescriptors.Add(new FilterDescriptor()
        {
            Member = nameof(GridModel.Description),
            MemberType = typeof(string),
            Operator = FilterOperator.Contains,
            Value = searchString
        });

        gridState.SearchFilter = cfd;

        await GridRef.SetStateAsync(gridState);
    }

    private async Task OnClearButtonClick()
    {
        if (GridRef is null)
        {
            return;
        }

        GridState<GridModel> gridState = GridRef.GetState();

        (gridState.SearchFilter as CompositeFilterDescriptor)?.FilterDescriptors.Clear();

        await GridRef.SetStateAsync(gridState);
    }

    protected override void OnInitialized()
    {
        for (int i = 1; i <= 500; i++)
        {
            GridData.Add(new GridModel()
            {
                Id = i,
                Name = $"{(char)Random.Shared.Next(65, 91)}{(char)Random.Shared.Next(65, 91)} " +
                    $"{(char)Random.Shared.Next(65, 91)}{(char)Random.Shared.Next(65, 91)} {i}",
                Description = $"{(char)Random.Shared.Next(97, 123)}{(char)Random.Shared.Next(97, 123)} " +
                    $"{(char)Random.Shared.Next(97, 123)}{(char)Random.Shared.Next(97, 123)} {i}"
            });
        }
    }

    public class GridModel
    {
        public int Id { get; set; }
        public string Name { get; set; } = string.Empty;
        public string Description { get; set; } = string.Empty;
    }
}
```

> tip If you want to set an initial state to the Grid, use a similar snippet, but in the [`OnStateInit event`](https://www.telerik.com/blazor-ui/documentation/components/grid/state#onstateinit)

## Customize the SearchBox

The [`GridToolBarSearchBoxTool`](https://www.telerik.com/blazor-ui/documentation/api/telerik.blazor.components.gridtoolbarsearchboxtool) and [`GridSearchBox`](https://www.telerik.com/blazor-ui/documentation/api/telerik.blazor.components.gridsearchbox) components offer the same variety of parameters to customize its behavior.

`DebounceDelay` sets the time in milliseconds between the user typing ends and the search starts. This provides a performance optimization when using the [`OnRead` event](https://www.telerik.com/blazor-ui/documentation/common-features/data-binding/onread). Filtering does not occur on every keystroke during fast typing, unless `DebounceDelay` is set to `0`.

The Grid searches in all visible string columns or a subset of theirs. It is also possible to programmatically [search in `string` fields, which are not displayed in the Grid](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-search-in-hidden-fields).

The example below demonstrates all SearchBox settings in action, and also how to move the SearchBox on the opposite side of the Grid ToolBar when using `GridToolBarTemplate`.

> caption Grid SearchBox customizaton

<demo metaUrl="client/grid/filter-searchbox-custom/" height="550"></demo>

## See Also

* [Live Demo: Grid Filter Searchbox](https://demos.telerik.com/blazor-ui/grid/searchbox)
* [Highlight or Bold Search Results in the Grid](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-search-highlight-results)
* [Search the Grid in Numeric and Date Model Fields](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-search-numeric-fields)
* [Search the Grid in Hidden Fields](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-search-in-hidden-fields)
* [Change Grid Search Results on Column Hide or Show](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-search-column-menu)
* [Search the Grid with a `StartsWith` Operator](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-search-startswith)
* [Search the Grid on Button Click](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-search-button-click)
* [Grid Filtering Overview](https://www.telerik.com/blazor-ui/documentation/components/grid/filter/overview)
* [Blazor Grid](https://www.telerik.com/blazor-ui/documentation/components/grid/overview)
