# Grid Data Binding

Telerik Blazor Grid is data source agnostic - you can use any database and service according to your project. You only need to get the collection of data models to the Grid in the view-model of the component hosting it.

# Basics

There are two main steps to data bind a Grid:

1. **Provide the data.** There are two ways to do this:
   * Set the Grid **`Data`** parameter. In this case, the Grid will hold **all the data** and will perform all data operations internally (sorting, paging, filtering, grouping, aggregates, etc.). This approach provides **simplicity**.
   * Use the Grid [`OnRead` event](https://www.telerik.com/blazor-ui/documentation/common-features/data-binding/onread) and pass the data to the event argument. In this case, the Grid will hold **only the current page** of data items. The component will fire `OnRead` again for every data operation and expect to receive the expected items. This approach provides **flexibility** and **performance** if the total number of data items is large. Also check the [Grid-specific **`OnRead`** event article](https://www.telerik.com/blazor-ui/documentation/components/grid/manual-operations).
2. **Configure the Grid to display the data.** Again, there are two ways to do this:
   * [Generate some or all columns automatically](https://www.telerik.com/blazor-ui/documentation/components/grid/columns/auto-generated). The autogenerated columns will depend on the Model properties and Data Annotations.
   * Define [GridColumn](https://www.telerik.com/blazor-ui/documentation/components/grid/columns/bound) instances and set their `Field` parameter. The field is required for data and CRUD operations. The field type determines the sorting, filtering and editing behavior. Some columns do not need a field, for example the [CommandColumn](https://www.telerik.com/blazor-ui/documentation/components/grid/columns/command). To customize data rendering and column behavior, use [templates](https://www.telerik.com/blazor-ui/documentation/components/grid/templates/overview) and all the [column settings](https://www.telerik.com/blazor-ui/documentation/components/grid/columns/bound#grid-bound-column-parameters).

## Data Sources and Scenarios

The following list of resources provides examples for data binding a grid in various scenarios:

* Basic **example**, tutorial **video** and **notes** - [Grid Bound Columns Overview](https://www.telerik.com/blazor-ui/documentation/components/grid/columns/bound). Also lists the features (parameters) of a bound column.

* **Optimizing the data source queries** - see the [Notes](https://www.telerik.com/blazor-ui/documentation/components/grid/columns/bound#notes) section in the article above. In a server-side app, an `IQueryable` that ties to an appropriate context (such as EntityFramework) that can optimize the LINQ queries the grid generates is a quick option. For full control, use the [OnRead event](https://www.telerik.com/blazor-ui/documentation/components/grid/manual-operations).

* **SQL** (or any other) **database** - you can find examples in our [online demos](https://demos.telerik.com/blazor-ui/grid/overview). You can see an offline version of the demos project in the `demos` folder of your installation ([automated](https://www.telerik.com/blazor-ui/documentation/installation/automated) or [archive](https://www.telerik.com/blazor-ui/documentation/installation/zip)). They showcase an EntityFramework context using an SQL database that provides data to a grid through a service, which is a common architecture for decoupling the front-end from the actual data source that you can apply to any database.

  * The **CRUD sample project** our extensions for [Visual Studio](https://www.telerik.com/blazor-ui/documentation/installation/vs-integration/new-project-wizard) and [Visual Studio Code](https://www.telerik.com/blazor-ui/documentation/installation/vs-code-integration/introduction) can generate for you showcases a similar architecture that you can use as a starting point.

  * The [Blazing Coffee sample application](https://github.com/telerik/blazor-ui/tree/master/sample-applications/blazing-coffee) shows how to provide data from a SQL (uses SQLite) database to the Grid using Entity Framework Core services.

* **WebAPI** data source - you can see how to send an appropriate request for data and return an optimized query in the following sample projects: [Grid DataSourceRequest on the server](https://github.com/telerik/blazor-ui/tree/master/grid/datasourcerequest-on-server). This is a flexible approach that you can use for any type of service you have - serializing and deserializing the data according to the application logic and needs, and optimizing the database queries on the backend.

* **OData** data source - an extension method we provide lets you make OData v4 queries as shown in the following example: [Grid and OData](https://github.com/telerik/blazor-ui/tree/master/grid/odata).

* **DataTable**, **ExpandoObject** collection - If you don't have actual strongly typed models (yet) and you use `ExpandoObject`, or your backend comes from an older technology and still returns `DataTable`s, the grid can accommodate such dynamic data types. You can get started from our examples on how to [bind the Grid to ExpandoObject collection](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-binding-to-expando-object) and [bind the Grid to a DataTable](https://demos.telerik.com/blazor-ui/grid/data-table), which also support [editing](https://www.telerik.com/blazor-ui/documentation/components/grid/editing/overview).

* **gRPC** - the gRPC tooling supports .NET Core, and as of mid-June 2020, there is a package that brings it to WebAssembly. You can find a basic example and more resources to get you started with gRPC in Blazor in the [Grid Data from gRPC Sample Project](https://github.com/telerik/blazor-ui/tree/master/common/grpc-example).

* **Foreign Keys** - using foreign tables and keys is usually done through the grid templates. You can read more and find examples in the [Grid - Foreign Key](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grids-foreign-key) KnowledgeBase article.

## Binding to Interface

The Grid supports binding to a collection of multiple model types that implement the same interface.

Note the usage of [`OnModelInit`](https://www.telerik.com/blazor-ui/documentation/components/grid/events#onmodelinit) in the example below. The event handler sets the model type to be used for new items in the Grid. One-type model creation is supported out-of-the-box. If you need to support adding instances of different types:

* Use custom **Add** buttons in the [Grid Toolbar](https://www.telerik.com/blazor-ui/documentation/components/grid/toolbar), one for each model type.
* In each button click handler, define an `InsertedItem` of the correct type in the [Grid State](https://www.telerik.com/blazor-ui/documentation/components/grid/state).
* [Put the Grid in Insert mode](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-add-edit-state) with the [SetStateAsync method](https://www.telerik.com/blazor-ui/documentation/components/grid/state#methods).

> caption Data Binding the Grid to an Interface

<demo metaUrl="client/grid/data-binding-interface/" height="500"></demo>

> note Up to version 2.26, the `Data` collection of the Grid must contain instances of only one model type.

## See Also

* [Live Demo: Bind Grid to Observable Data](https://demos.telerik.com/blazor-ui/grid/observable-data)
* [Live Demo: Bind Grid to DataTable](https://demos.telerik.com/blazor-ui/grid/data-table)
* [Live Demo: Manual Grid Data Operations](https://demos.telerik.com/blazor-ui/grid/manual-operations)
* [Blazor Grid](https://www.telerik.com/blazor-ui/documentation/components/grid/overview)
