# Databinding with the OnRead Event

This article presents the `OnRead` event and describes how to use it to data bind Telerik Blazor components.

* [Purpose and benefits of `OnRead`](https://www.telerik.com/blazor-ui/documentation/common-features/data-binding/onread#purpose-and-benefits)
* [Which Blazor components use `OnRead`](https://www.telerik.com/blazor-ui/documentation/common-features/data-binding/onread#components-with-onread-event)
* [Description of the `OnRead` event argument](https://www.telerik.com/blazor-ui/documentation/common-features/data-binding/onread#event-argument)
* [How to return data in `OnRead` with the `ToDataSourceResult` method](https://www.telerik.com/blazor-ui/documentation/common-features/data-binding/onread#todatasourceresult-method)
* [Example](https://www.telerik.com/blazor-ui/documentation/common-features/data-binding/onread#example)
* [How to refresh the component data when using `OnRead`](https://www.telerik.com/blazor-ui/documentation/common-features/data-binding/onread#refresh-data)

## Purpose and Benefits

The easiest way to provide data to a component is to set its `Data` parameter to `IEnumerable<T>`. This allows the component to have all data items and to perform all data operations internally (filtering, paging, sorting, etc.). However, this scenario is not always the most optimal.

There are two main reasons to use the `OnRead` event: **performance** and **customization**.

### Performance

Large amounts of data require loading in chunks and on demand. This improves the performance of the database, backend, network, and the browser. When a component fires `OnRead`, it expects to receive **only the data items to render**. The exact number depends on the component's `PageSize` parameter.

`OnRead` also offloads data operations outside the component, for example on the remote server. This can improve WebAssembly application performance.

### Customization

`OnRead` allows full control over the data operations. For example, it is possible to use custom sorting and filtering algorithms, if the built-in ones do not fit a given scenario. Here are just a few examples, but there are many more possible scenarios:

* [Search by multiple data fields in ComboBox and DropDownList](https://www.telerik.com/blazor-ui/documentation/knowledge-base/dropdowns-search-in-multiple-fields)
* [Search in hidden Grid columns](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-search-in-hidden-fields)
* [Debounce Grid data requests](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-debounce-operations)
* [Debounce ComboBox filter requests](https://www.telerik.com/blazor-ui/documentation/knowledge-base/combo-debounce-onread)

`OnRead` enables [data binding to **OData** services](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-odata).

`OnRead` also allows the application to know the exact data items, which the user is currently seeing.

## Components with OnRead Event

The following Blazor components expose an `OnRead` event. To gain **performance** benefits, use the event together with **paging** or **virtualization** (also called virtual scrolling).

Each component name points to component-specific `OnRead` documentation and examples:

| Component                                                                                                           | Supports Paging                                                                               | Supports Virtualization                                                                                                             |
| ------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| [AutoComplete](https://www.telerik.com/blazor-ui/documentation/components/autocomplete/events#onread)               | -                                                                                             | [AutoComplete virtualization](https://www.telerik.com/blazor-ui/documentation/components/autocomplete/virtualization)               |
| [ComboBox](https://www.telerik.com/blazor-ui/documentation/components/combobox/events#onread)                       | -                                                                                             | [ComboBox virtualization](https://www.telerik.com/blazor-ui/documentation/components/combobox/virtualization)                       |
| [DropDownList](https://www.telerik.com/blazor-ui/documentation/components/dropdownlist/events#onread)               | -                                                                                             | [DropDownList virtualization](https://www.telerik.com/blazor-ui/documentation/components/dropdownlist/virtualization)               |
| [Grid](https://www.telerik.com/blazor-ui/documentation/components/grid/manual-operations)                           | [Grid paging](https://www.telerik.com/blazor-ui/documentation/components/grid/paging)         | [Grid row virtualization](https://www.telerik.com/blazor-ui/documentation/components/grid/virtual-scrolling)                        |
| [ListView](https://www.telerik.com/blazor-ui/documentation/components/listview/manual-operations)                   | [ListView paging](https://www.telerik.com/blazor-ui/documentation/components/listview/paging) | -                                                                                                                                   |
| [MultiColumnComboBox](https://www.telerik.com/blazor-ui/documentation/components/multicolumncombobox/events#onread) | -                                                                                             | [MultiColumnComboBox virtualization](https://www.telerik.com/blazor-ui/documentation/components/multicolumncombobox/virtualization) |
| [MultiSelect](https://www.telerik.com/blazor-ui/documentation/components/multiselect/events#onread)                 | -                                                                                             | [MultiSelect virtualization](https://www.telerik.com/blazor-ui/documentation/components/multiselect/virtualization)                 |

Components like the [**TreeList**](https://www.telerik.com/blazor-ui/documentation/components/treelist/data-binding/load-on-demand) and the [**TreeView**](https://www.telerik.com/blazor-ui/documentation/components/treeview/data-binding/load-on-demand) don't have an `OnRead` event. Instead, they load data on demand via `OnExpand` events.

## Event Argument

The `OnRead` event handler receives an argument, which inherits from [`ReadEventArgs`](https://www.telerik.com/blazor-ui/documentation/api/telerik.blazor.components.readeventargs). The exact type depends on the component. For example, the Grid handler receives `GridReadEventArgs`. The ComboBox handler receives `ComboBoxReadEventArgs`, and so on.

The following properties of the event argument object are common for all [components with an `OnRead` event](https://www.telerik.com/blazor-ui/documentation/common-features/data-binding/onread#components-with-onread-event). Other properties are discussed in component-specific articles.

<style>
    .article table {
        table-layout: auto;
        word-break: normal;
    }
</style>

| Property  | Type                                                                                                            | Description                                                                                                                                                   |
| --------- | --------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Request` | [`DataSourceRequest`](https://www.telerik.com/blazor-ui/documentation/api/telerik.datasource.datasourcerequest) | This object carries information about the requested data items. It will reveal the page index or virtual scroll offset, the sorting and filtering state, etc. |
| `Data`    | `IEnumerable`                                                                                                   | Set it to the **chunk** of data items, which the component will **render**.                                                                                   |
| `Total`   | `int`                                                                                                           | Set it to the **total number** of items. This value will help the component generate its **pager** or **virtual scrollbar** correctly.                        |

> caption Using DataSourceRequest properties

```CS.skip-repl
async Task GridReadHandler(GridReadEventArgs args)
{
    // What is the new page?
    // args.Request.Page

    // What is the page size (how many items to return)?
    // args.Request.PageSize

    // How many rows the user has scrolled in virtual scenarios?
    // args.Request.Skip
}
```

> tip The [`DataSourceRequest` object can be serialized and sent to the remote server](https://github.com/telerik/blazor-ui/tree/master/grid/datasourcerequest-on-server). **Use the `System.Text.Json` serializer**.

## ToDataSourceResult Method

The [`DataSourceRequest` object](https://www.telerik.com/blazor-ui/documentation/api/telerik.datasource.datasourcerequest) provides information about the needed data. The question is how to retrieve this data most easily. Sometimes `OnRead` data binding is called "manual", but in most cases it doesn't have to be manual at all. The solution is **`ToDataSourceResult`**.

The `ToDataSourceResult` extension method is able to extract the requested data items from `IEnumerable`, `IQueryable` and `DataTable`. The method is part of the [Telerik.DataSource.Extensions](https://www.telerik.com/blazor-ui/documentation/api/telerik.datasource.extensions) namespace. It expects a `DataSourceRequest` argument.

`ToDataSourceResult` returns a [`DataSourceResult` object](https://www.telerik.com/blazor-ui/documentation/api/telerik.datasource.datasourceresult). Its most important properties are:

| Property | Type          | Description                                                                                                  |
| -------- | ------------- | ------------------------------------------------------------------------------------------------------------ |
| `Data`   | `IEnumerable` | The chunk (page) of data items to render. All data operations are already applied (sorting, filtering, etc.) |
| `Total`  | `int`         | The total number of items in the datasource.                                                                 |

The `Data` and `Total` properties of the `DataSourceRequest` and `DataSourceResult` match, and allow easy value assignment:

> caption Using ToDataSourceResult

```CS.skip-repl
using Telerik.DataSource.Extensions;

private IEnumerable<GridModel> AllGridData { get; set; }

private async Task GridReadHandler(GridReadEventArgs args)
{
    DataSourceResult result = AllGridData.ToDataSourceResult(args.Request);

    args.Data = result.Data;
    args.Total = result.Total;
    args.AggregateResults = result.AggregateResults; // Grid only
}
```

**`ToDataSourceResultAsync`** is the awaitable (asynchronous) alternative of `ToDataSourceResult`.

> tip It is possible to use `DataSourceRequest`, `ToDataSourceResult` and `ToDataSourceResultAsync` in scenarios, which are not related to a specific Telerik component.

## Example

Let's imagine that our datasource contains 1,000 items, and we want to send only one page of items to a Grid.

1. Import the `Telerik.DataSource.Extensions` namespace.
2. Set the Grid's `TItem` parameter to the model type. (Some components require a `TValue` parameter to define the value type, but not the Grid. Use `TValue` with the AutoComplete, ComboBox, DropDownList, and MultiSelect.)
3. Subscribe to the `OnRead` event. [**Use `async Task` and not `async void`**](https://docs.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming#avoid-async-void). The event handler receives a `GridReadEventArgs` argument. Let's name it `args`.
4. Use `args.Request` and `ToDataSourceResult()` to get one page of Grid data. The data may be filtered and sorted, based on the user's actions.
5. Set `args.Data` to the data items to render.
6. Set `args.Total` to the total number of data items (1000).

> caption Using the OnRead event

```RAZOR
@using Telerik.DataSource.Extensions

<TelerikGrid TItem="@SampleModel"
             OnRead="@OnGridRead"
             AutoGenerateColumns="true"
             Sortable="true"
             Pageable="true"
             FilterMode="@GridFilterMode.FilterRow"
             Height="400px" />

<p> OnGridRead fired at: @LastOnRead </p>

@code {
    private List<SampleModel> GridData { get; set; }

    private string LastOnRead { get; set; }

    // always use async Task, and not async void
    private async Task OnGridRead(GridReadEventArgs args)
    {
        var result = GridData.ToDataSourceResult(args.Request);
        args.Data = result.Data;
        args.Total = result.Total;

        var now = DateTime.Now;
        LastOnRead = now.ToLongTimeString() + "." + now.Millisecond;
    }

    protected override void OnInitialized()
    {
        GenerateData();

        base.OnInitialized();
    }

    private void GenerateData()
    {
        GridData = new List<SampleModel>();

        for (int i = 1; i <= 1000; i++)
        {
            GridData.Add(new SampleModel() { Id = i, Text = $"Grid Text {i}" });
        }
    }

    public class SampleModel
    {
        public int Id { get; set; }
        public string Text { get; set; }
    }
}
```

## Refresh Data

The components fire an `OnRead` event when the user performs an action, such as paging, sorting, virtual scrolling, etc. Calling the `OnRead` handler manually will not have effect, because the component will not be tracking the method arguments.

All components with an `OnRead` event have a `Rebind` method as well. To refresh the component data programmatically, call this method. It will force the component to fire `OnRead` and receive new data.

Also check [how to rebind and refresh a component with a `Timer`](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-rebind-timer).

> caption Rebind DropDownList and Grid when using OnRead

```RAZOR
@using Telerik.DataSource.Extensions

<TelerikButton ThemeColor="@ThemeConstants.Button.ThemeColor.Primary"
               OnClick="@RebindComponents">Rebind Components</TelerikButton>

<br />
<br />

<TelerikDropDownList @ref="@DropDownListRef"
                     TItem="@SampleModel"
                     TValue="@int"
                     OnRead="@OnDropDownRead"
                     @bind-Value="@DropDownValue"
                     ValueField="@nameof(SampleModel.Id)"
                     TextField="@nameof(SampleModel.Text)"
                     Width="200px">
    <ValueTemplate>
        @context.Id : @context.Text
    </ValueTemplate>
    <ItemTemplate>
        @context.Id : @context.Text
    </ItemTemplate>
</TelerikDropDownList>

<br />
<br />

<TelerikGrid @ref="@GridRef"
             TItem="@SampleModel"
             OnRead="@OnGridRead"
             AutoGenerateColumns="true"
             Sortable="true"
             Pageable="true"
             PageSize="5" />

@code {
    private TelerikGrid<SampleModel>? GridRef { get; set; }
    private TelerikDropDownList<SampleModel, int>? DropDownListRef { get; set; }

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

    private int DropDownValue { get; set; } = 1;

    private int ItemCounter { get; set; } = 3;

    private void RebindComponents()
    {
        GenerateData(); // simulate data change

        GridRef?.Rebind();
        DropDownListRef?.Rebind();
    }

    private async Task OnGridRead(GridReadEventArgs args)
    {
        var result = await GridData.ToDataSourceResultAsync(args.Request);
        args.Data = result.Data;
        args.Total = result.Total;
    }

    private async Task OnDropDownRead(DropDownListReadEventArgs args)
    {
        var result = await DropDownData.ToDataSourceResultAsync(args.Request);
        args.Data = result.Data;
        args.Total = result.Total;
    }

    protected override void OnInitialized()
    {
        GenerateData();

        base.OnInitialized();
    }

    private void GenerateData()
    {
        GridData = new List<SampleModel>();
        DropDownData = new List<SampleModel>();

        for (int i = 1; i <= ItemCounter; i++)
        {
            GridData.Add(new SampleModel() { Id = i, Text = $"Text {(char)Random.Shared.Next(65, 91)}{(char)Random.Shared.Next(65, 91)}" });
            DropDownData.Add(new SampleModel() { Id = i, Text = $"Text {(char)Random.Shared.Next(65, 91)}{(char)Random.Shared.Next(65, 91)}" });
        }

        ItemCounter++;
    }

    public class SampleModel
    {
        public int Id { get; set; }
        public string Text { get; set; } = string.Empty;
    }
}
```

## See Also

* [Using the Grid with OnRead](https://www.telerik.com/blazor-ui/documentation/components/grid/manual-operations)
* [Data Binding to cloud services](https://www.telerik.com/blazor-ui/documentation/common-features/data-binding/cloud-services)
