|
| 1 | +--- |
| 2 | +title: Data Binding |
| 3 | +page_title: Data Binding | Grid for Blazor |
| 4 | +description: Data bind the Blazor Grid to any of the supported data sources. |
| 5 | +slug: grid-data-binding |
| 6 | +tags: telerik,blazor,grid,data,binding,databind,databinding |
| 7 | +published: true |
| 8 | +position: 10 |
| 9 | +--- |
| 10 | + |
| 11 | +# Grid Data Binding |
| 12 | + |
| 13 | +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. |
| 14 | + |
| 15 | +## Data Sources and Scenarios |
| 16 | + |
| 17 | +The following list of resources provides examples for data binding a grid in various scenarios: |
| 18 | + |
| 19 | +* Basic **example**, tutorial **video** and **notes** - [Grid Bound Columns Overview]({%slug components/grid/columns/bound%}). Also lists the features (parameters) of a bound column. |
| 20 | + |
| 21 | +* **Optimizing the data source queries** - see the [Notes]({%slug components/grid/columns/bound%}#notes) section in the article above. In a server-side app, an `IQueriable` 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]({%slug components/grid/manual-operations%}). |
| 22 | + |
| 23 | +* **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]({%slug installation/msi%}) or [archive]({%slug 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 decouping the front-end from the actual data source that you can apply to any database. |
| 24 | + |
| 25 | + * The **CRUD sample project** our extensions for [Visual Studio]({%slug getting-started-vs-integration-new-project%}) and [Visual Studio Code]({%slug getting-started-vs-code-integration-overview%}) can generate for you showcases a similar architecture that you can use as a starting point. |
| 26 | + |
| 27 | + * 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. |
| 28 | + |
| 29 | +* **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. |
| 30 | + |
| 31 | +* **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). |
| 32 | + |
| 33 | +* **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 a [ExpandoObject collection](https://github.com/telerik/blazor-ui/tree/master/grid/binding-to-expando-object) and to a [DataTable](https://demos.telerik.com/blazor-ui/grid/data-table) which also support [editing]({%slug components/grid/overview%}#editing). |
| 34 | + |
| 35 | +* **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). |
| 36 | + |
| 37 | +* **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]({%slug grids-foreign-key%}) KnowledgeBase article. |
| 38 | + |
| 39 | +## Binding to Interface |
| 40 | + |
| 41 | +Since version 2.27, the Grid supports binding to a collection of multiple model types that implement the same interface. |
| 42 | + |
| 43 | +Note the usage of [`OnModelInit`]({%slug 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: |
| 44 | + |
| 45 | +* Use custom **Add** buttons in the [Grid Toolbar]({%slug components/grid/features/toolbar%}), one for each model type. |
| 46 | +* In each button click handler, define an `InsertedItem` of the correct type in the [Grid State]({%slug grid-state%}). |
| 47 | +* [Put the Grid in Insert mode]({%slug grid-state%}#initiate-editing-or-inserting-of-an-item) with the [SetState method]({%slug grid-state%}#methods). |
| 48 | + |
| 49 | +>caption Data Binding the Grid to an Interface |
| 50 | +
|
| 51 | +````CSHTML |
| 52 | +<TelerikGrid Data="@GridData" |
| 53 | + FilterMode="GridFilterMode.FilterRow" |
| 54 | + EditMode="GridEditMode.Inline" |
| 55 | + OnUpdate="@UpdateHandler" |
| 56 | + OnDelete="@DeleteHandler" |
| 57 | + OnCreate="@CreateHandler" |
| 58 | + OnModelInit="@(() => new Model1())"> |
| 59 | + <GridToolBar> |
| 60 | + <GridCommandButton Command="Add" Icon="add">Add</GridCommandButton> |
| 61 | + </GridToolBar> |
| 62 | + <GridColumns> |
| 63 | + <GridColumn Field="IntProperty" /> |
| 64 | + <GridCommandColumn> |
| 65 | + <GridCommandButton Command="Edit">Edit</GridCommandButton> |
| 66 | + <GridCommandButton Command="Save" ShowInEdit="true">Save</GridCommandButton> |
| 67 | + <GridCommandButton Command="Cancel" ShowInEdit="true">Cancel</GridCommandButton> |
| 68 | + </GridCommandColumn> |
| 69 | + </GridColumns> |
| 70 | +</TelerikGrid> |
| 71 | +
|
| 72 | +@code { |
| 73 | + public interface IModel |
| 74 | + { |
| 75 | + public int Id { get; set; } |
| 76 | + public int IntProperty { get; set; } |
| 77 | + } |
| 78 | +
|
| 79 | + public class Model1 : IModel |
| 80 | + { |
| 81 | + public int Id { get; set; } |
| 82 | + public int IntProperty { get; set; } |
| 83 | + } |
| 84 | +
|
| 85 | + public class Model2 : IModel |
| 86 | + { |
| 87 | + public int Id { get; set; } |
| 88 | + public int IntProperty { get; set; } |
| 89 | + } |
| 90 | +
|
| 91 | + List<IModel> GridData { get; set; } |
| 92 | +
|
| 93 | + protected override void OnInitialized() |
| 94 | + { |
| 95 | + var data = new List<IModel>(); |
| 96 | +
|
| 97 | + data.Add(new Model1() |
| 98 | + { |
| 99 | + Id = 1, |
| 100 | + IntProperty = 1 |
| 101 | + }); |
| 102 | + data.Add(new Model2() |
| 103 | + { |
| 104 | + Id = 2, |
| 105 | + IntProperty = 2 |
| 106 | + }); |
| 107 | +
|
| 108 | + GridData = data; |
| 109 | + } |
| 110 | +
|
| 111 | + public void UpdateHandler(GridCommandEventArgs args) |
| 112 | + { |
| 113 | + var model = (IModel)args.Item; |
| 114 | +
|
| 115 | + var matchingItem = GridData.FirstOrDefault(c => c.Id == model.Id); |
| 116 | +
|
| 117 | + if (matchingItem != null) |
| 118 | + { |
| 119 | + matchingItem.IntProperty = model.IntProperty; |
| 120 | + } |
| 121 | + } |
| 122 | +
|
| 123 | + public void DeleteHandler(GridCommandEventArgs args) |
| 124 | + { |
| 125 | + var model = (IModel)args.Item; |
| 126 | +
|
| 127 | + GridData.Remove(model); |
| 128 | + } |
| 129 | +
|
| 130 | + public void CreateHandler(GridCommandEventArgs args) |
| 131 | + { |
| 132 | + var model = (IModel)args.Item; |
| 133 | +
|
| 134 | + model.Id = GridData.Max(d => d.Id) + 1; |
| 135 | +
|
| 136 | + GridData.Insert(0, model); |
| 137 | + } |
| 138 | +} |
| 139 | +```` |
| 140 | + |
| 141 | +>note Up to version 2.26, the `Data` collection of the Grid must contain instances of only one model type. |
| 142 | +
|
| 143 | +## See Also |
| 144 | + |
| 145 | + * [Live Demo: Bind Grid to Observable Data](https://demos.telerik.com/blazor-ui/grid/observable-data) |
| 146 | + * [Live Demo: Bind Grid to DataTable](https://demos.telerik.com/blazor-ui/grid/data-table) |
| 147 | + * [Live Demo: Manual Grid Data Operations](https://demos.telerik.com/blazor-ui/grid/manual-operations) |
0 commit comments