# Highlight, Format or Bold Grid Search Results

## Environment

<table>
    <tbody>
        <tr>
            <td>Product</td>
            <td>Grid for Blazor, <br />
            TreeList for Blazor</td>
        </tr>
    </tbody>
</table>

## Description

I am using the Grid [**SearchBox**](https://www.telerik.com/blazor-ui/documentation/components/grid/filter/searchbox). How to format the Grid cell values to bold the search keyword? Is there a suitable event to highlight the text, which matches the search string in the cells?

How to highlight matching search results in the Grid cells?

## Solution

The following approach is valid for both the Grid and the TreeList.

1. Define [column templates](https://www.telerik.com/blazor-ui/documentation/components/grid/templates/column) for all string columns in the Grid.
2. Inside the column template, obtain the search string from the [Grid state](https://www.telerik.com/blazor-ui/documentation/components/grid/state):
   * Check the `SearchFilter` property of the [`GridState` object](https://www.telerik.com/blazor-ui/documentation/api/telerik.blazor.components.gridstate-1).
   * It will contain a [`CompositeFilterDescriptor` with multiple nested `FilterDescriptor`s inside](https://www.telerik.com/blazor-ui/documentation/components/grid/filter/overview#filter-descriptors).
   * The search string will be the `Value` of any nested `FilterDescriptor` object.
   * The example below uses the [Grid `OnStateChanged` event](https://www.telerik.com/blazor-ui/documentation/components/grid/state#events) to cache the search string.
3. Insert HTML tags inside the column template that will format or style the cell content:
   * Use some string manipulation or replacement.
   * Render the cell content with a [`MarkupString`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.markupstring), otherwise the Blazor framework will encode the tags and the user will see them.
   * Decide how you want to handle letter casing during string replacement.

> caption Format or highlight Grid search results

```RAZOR
@using Telerik.DataSource

<TelerikGrid @ref="@Grid"
             Data="@GridData"
             TItem="@GridModel"
             Pageable="true"
             OnStateChanged="@( (GridStateEventArgs<GridModel> args) => OnGridStateChanged(args) )">
    <GridToolBarTemplate>
        Type a letter or a number: <GridSearchBox />
    </GridToolBarTemplate>
    <GridColumns>
        <GridColumn Field="@nameof(GridModel.Text1)">
            <Template>
                @{
                    GridModel item = context as GridModel;

                    if (!String.IsNullOrEmpty(SearchBoxValue))
                    {
                        @(new MarkupString(item.Text1.Replace(SearchBoxValue,
                            $"<strong>{SearchBoxValue}</strong>",
                            StringComparison.InvariantCultureIgnoreCase)))
                    }
                    else
                    {
                        @item.Text1
                    }
                }
            </Template>
        </GridColumn>
        <GridColumn Field="@nameof(GridModel.Text2)">
            <Template>
                @{
                    GridModel item = context as GridModel;

                    if (!String.IsNullOrEmpty(SearchBoxValue))
                    {
                        @(new MarkupString(item.Text2.Replace(SearchBoxValue,
                            $"<strong>{SearchBoxValue.ToUpperInvariant()}</strong>",
                            StringComparison.InvariantCultureIgnoreCase)))
                    }
                    else
                    {
                        @item.Text2
                    }
                }
            </Template>
        </GridColumn>
    </GridColumns>
</TelerikGrid>

<style>
    td strong {
        color: red;
        background: yellow;
    }
</style>

@code {
    List<GridModel> GridData { get; set; }
    TelerikGrid<GridModel> Grid { get; set; }
    string SearchBoxValue { get; set; }

    void OnGridStateChanged(GridStateEventArgs<GridModel> args)
    {
        if (args.PropertyName == "SearchFilter")
        {
            CompositeFilterDescriptor searchDescriptor = Grid.GetState().SearchFilter as CompositeFilterDescriptor;
            string searchString = (searchDescriptor?.FilterDescriptors.FirstOrDefault() as FilterDescriptor)?.Value.ToString();

            SearchBoxValue = searchString ?? String.Empty;
        }
    }

    protected override void OnInitialized()
    {
        GridData = new List<GridModel>();
        var rnd = new Random();

        for (int i = 1; i <= 300; i++)
        {
            GridData.Add(new GridModel()
            {
                Id = i,
                Text1 = $"{(char)rnd.Next(65, 91)}{(char)rnd.Next(65, 91)}{(char)rnd.Next(65, 91)} {(i * 11)}",
                Text2 = $"{(char)rnd.Next(65, 91)}{(char)rnd.Next(65, 91)}{(char)rnd.Next(65, 91)} {(i * 22)}"
            });
        }
    }

    public class GridModel
    {
        public int Id { get; set; }
        public string Text1 { get; set; }
        public string Text2 { get; set; }
    }
}
```

## See Also

* [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)
* [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)
