Example T1154623
Visible to All Users

Grid for Blazor - How to bind the component to data with Entity Framework Core

This example demonstrates how to use Entity Framework Core to bind the DevExpress Blazor Grid to data.

Bind DevExpress Blazor Grid to Data with Entity Framework Core

Refer to the following topic for more information: Bind Components to Data with Entity Framework Core.

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Pages/Index.razor
Razor
@page "/" @using Microsoft.EntityFrameworkCore @using BindToData.Models @inject IDbContextFactory<NorthwindContext> NorthwindContextFactory @implements IDisposable <DxGrid Data="Data" CustomizeEditModel="OnCustomizeEditModel" EditModelSaving="OnEditModelSaving" DataItemDeleting="OnDataItemDeleting" KeyFieldName="EmployeeId"> <Columns> <DxGridCommandColumn /> <DxGridDataColumn FieldName="FirstName" /> <DxGridDataColumn FieldName="LastName" /> <DxGridDataColumn FieldName="Title" /> <DxGridDataColumn FieldName="HireDate" /> </Columns> <EditFormTemplate Context="editFormContext"> <DxFormLayout> <DxFormLayoutItem Caption="First Name:"> @editFormContext.GetEditor("FirstName") </DxFormLayoutItem> <DxFormLayoutItem Caption="Last Name:"> @editFormContext.GetEditor("LastName") </DxFormLayoutItem> <DxFormLayoutItem Caption="Title:"> @editFormContext.GetEditor("Title") </DxFormLayoutItem> <DxFormLayoutItem Caption="Hire Date:"> @editFormContext.GetEditor("HireDate") </DxFormLayoutItem> </DxFormLayout> </EditFormTemplate> </DxGrid> @code { IEnumerable<object> Data { get; set; } NorthwindContext Northwind { get; set; } protected override async Task OnInitializedAsync() { Northwind = NorthwindContextFactory.CreateDbContext(); Data = await Northwind.Employees.ToListAsync(); } void OnCustomizeEditModel(GridCustomizeEditModelEventArgs e) { if(e.IsNew) { var editModel = (Employee)e.EditModel; editModel.EmployeeId = Data.Count() + 1; } } async Task OnEditModelSaving(GridEditModelSavingEventArgs e) { var editModel = (Employee)e.EditModel; // Re-query a data item from the database. var dataItem = e.IsNew ? editModel : Northwind.Employees.Find(editModel.EmployeeId); // Assign changes from the edit model to the data item. if (dataItem != null) { dataItem.FirstName = editModel.FirstName; dataItem.LastName = editModel.LastName; dataItem.Title = editModel.Title; dataItem.HireDate = editModel.HireDate; dataItem.EmployeeId = editModel.EmployeeId; // Post changes to the database. if (e.IsNew) await Northwind.AddAsync(dataItem); await Northwind.SaveChangesAsync(); // Reload the entire Grid. Data = await Northwind.Employees.ToListAsync(); } } async Task OnDataItemDeleting(GridDataItemDeletingEventArgs e) { // Re-query a data item from the database. var dataItem = Northwind.Employees.Find((e.DataItem as Employee).EmployeeId); if (dataItem != null) { // Remove the data item from the database. Northwind.Remove(dataItem); await Northwind.SaveChangesAsync(); // Reload the entire Grid. Data = await Northwind.Employees.ToListAsync(); } } public void Dispose() { Northwind?.Dispose(); } }
Program.cs
C#
using BindToData.Models; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); builder.Services.AddDevExpressBlazor(options => { options.BootstrapVersion = DevExpress.Blazor.BootstrapVersion.v5; options.SizeMode = DevExpress.Blazor.SizeMode.Medium; }); builder.Services.AddDbContextFactory<NorthwindContext>((sp, options) => { var env = sp.GetRequiredService<IWebHostEnvironment>(); var dbPath = Path.Combine(env.ContentRootPath, "Northwind.db"); options.UseSqlite("DataSource=" + dbPath); }); builder.WebHost.UseWebRoot("wwwroot"); builder.WebHost.UseStaticWebAssets(); var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.MapBlazorHub(); app.MapFallbackToPage("/_Host"); app.Run();

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.