# Grid - Refresh Data

One of the key features of Telerik UI for Blazor Grid component is its ability to refresh the displayed data. This ensures that the Grid always presents the most up-to-date information, no matter if you are working with large datasets or frequently changing data sources. Understanding how to efficiently refresh the data in the Grid is crucial for maintaining both optimal performance and user experience. This guide covers various methods and best practices for refreshing data in the Telerik UI for Blazor Grid, helping you choose the right approach for your specific application needs related to [Blazor DataGrid](https://demos.telerik.com/blazor-ui/grid/overview) live update.

In this article:

* [Rebind Method](https://www.telerik.com/blazor-ui/documentation/components/grid/refresh-data#rebind-method)
* [Observable Data](https://www.telerik.com/blazor-ui/documentation/components/grid/refresh-data#observable-data)
* [New Collection Reference](https://www.telerik.com/blazor-ui/documentation/components/grid/refresh-data#new-collection-reference)
* [Call OnRead](https://www.telerik.com/blazor-ui/documentation/components/grid/refresh-data#call-onread)
* [Entity Framework Data](https://www.telerik.com/blazor-ui/documentation/components/grid/refresh-data#entity-framework-data)

## Rebind Method

You can refresh the Grid data by using the `Rebind` method exposed to the reference of the TelerikGrid. If you have manually defined the [OnRead event](https://www.telerik.com/blazor-ui/documentation/components/grid/manual-operations) the business logic defined in its event handler will be executed.

<demo metaUrl="client/grid/refresh-rebind/" height="500"></demo>

> note As part of our `3.0.1` release we introduced the `Rebind` method to the component reference. This can make some of the approaches in this article obsolete, especially resetting the collection reference.

## Observable Data

The most common reason you would use an ObservableCollection is to make a component (like a grid, treeview, treelist, dropdown) change or react when you change that collection.

When you want to refresh the component data source like that, there are two important framework behaviors you need to be aware of - when ObservableCollection instances fire events, and how to refresh the data of a component when it is not an observable collection.

Databound components can benefit from live updates - when the data source collection changes, the components should update to reflect that change. Most data-bound components in the Telerik UI for Blazor suite implement such functionality.

When the `Data` of the component is a collection that implements the `INotifyCollectionChanged` interface (such as `ObservableCollection`), the Telerik components subscribe to its `CollectionChanged` event to make live update. This means that adding items, removing items, or clearing the collection updates the components (its `.Add()`, `.Remove()` and `.Clear()` methods).

The Observable collections fire the `CollectionChanged` event only when their `Add`, `Remove` and `Clear` methods are called. They do not fire it when you change the value of a field of one of their elements.

> important Observable data is not supported with manual data operations via the `OnRead` event.

> caption Bind the Grid to an ObservableCollection, so it can react to collection changes.

<demo metaUrl="client/grid/refresh-observable/" height="500"></demo>

> note If you need to add/remove many items to/from the collection, consider creating a new collection and provide its reference to the data parameter. Thus, the component will re-render only once (when the data collection reference is changed) instead of re-rendering multiple times in response to the Add/Remove events.

## New Collection Reference

In Blazor, the framework will fire the `OnParametersSet` event of a child component (which is how child components can react to outside changes) only when it can detect a change in the object it receives through the corresponding parameter (like `Data` for the data sources of Telerik components). This detection works as follows:

* For strings and [value types](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-types), this happens when their value changes.

* For [reference types](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/reference-types) (such as data collections like `List`, or any `IEnumerable`, and application-specific objects), this happens when the object reference changes.

  Thus, you would usually need to create a `new` reference for the view-model field (such as `TreeViewData = new List<MyTreeViewItem>(theUpdatedDataCollection);`) when you want the component to update.

> caption Create new collection reference to refresh the Grid data.

<demo metaUrl="client/grid/refresh-new-collection/" height="500"></demo>

> note You can find some more explanations and examples for the Grid component in the [Force a Grid to Refresh](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-force-refresh) Knowledge Base article.

## Call OnRead

If you are [using the `OnRead` event to optimize the data requests](https://www.telerik.com/blazor-ui/documentation/components/grid/manual-operations), you may want to call that to fetch fresh data from the server. The Grid does that automatically after the [edit operations](https://www.telerik.com/blazor-ui/documentation/components/grid/editing/overview), yet you may need to do it on your own.

To make the Grid fire `OnRead`, execute its [`SetStateAsync` method](https://www.telerik.com/blazor-ui/documentation/components/grid/state)

> caption Make the grid call OnRead by using its state

<demo metaUrl="client/grid/refresh-on-read/" height="500"></demo>

## Entity Framework Data

When you use EF contexts to update your data, you may update or insert an item through the entity, but you may not see it updated in the grid. Code similar to the following may cause such behavior:

> caption Updating an entity alone may not update the data in the grid

```RAZOR
async Task UpdateHandler(GridCommandEventArgs args)
{
    using var dbContext = contextFactory.CreateDbContext();
    MyModel item = (MyModel)args.Items.First();
    var original = await dbContext.MyTable.FindAsync(item.Id);
    if (original != null)
    {
        dbContext.Entry(original).CurrentValues.SetValues(item);
        await dbContext.SaveChangesAsync();
    }
}
```

The reason for such behavior is that the `Data` collection of the grid is a separate in-memory copy of the database data, and the context will update only the database, not all in-memory copies.

This means that you need to ensure that the view-model will be updated as well, so that the `Data` parameter of the grid changes too. There are, generally, two ways to do this:

* Use the [`OnRead` event to perform the Grid data operations](https://www.telerik.com/blazor-ui/documentation/components/grid/manual-operations). The Grid [fires `OnRead` automatically after edit operations](https://www.telerik.com/blazor-ui/documentation/components/grid/editing/overview#onread-event) and this allows the app to query the database for the already updated data (which will also bring in other updates that other uses may have made).

* Update the local view-model data yourself with the information the grid event gives you (e.g., insert the new item in it, or remove a deleted item, or update the fields of an edited item). You can find similar code used in the [Grid - Inline Editing Live Demo](https://demos.telerik.com/blazor-ui/grid/editing-inline).

  * It is important to ensure the change happens on the object that the Grid uses. Methods like `.FirstOrDefault()` will return a new reference and changing them will not trigger a UI update.

## See Also

* [ObservableCollection](https://www.telerik.com/blazor-ui/documentation/common-features/data-binding/observable-data)
* [INotifyCollectionChanged Interface](https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.inotifycollectionchanged?view=netframework-4.8)
* [Live Demos](https://demos.telerik.com/blazor-ui)
* [Knowledge Base: Force a Grid to Refresh](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-force-refresh)
* [Blazor Grid](https://www.telerik.com/blazor-ui/documentation/components/grid/overview)
