Example E3252
Visible to All Users

Grid View for ASP.NET MVC - How to bind grid to Entity Framework in regular and database server modes

This example demonstrates two approaches on how to bind a grid to Entity Framework. The first approach is recommended for small data sources because it downloads the data to the grid. The second approach operates on the database side and is recommended for large amounts of data.

Grid mode

In grid mode, all data shaping operations are performed on the WebServer/Grid side. This mode is best suited for maintaining a small amount of data. You should explicitly evaluate data from the Entity Framework context used as the grid Model. In this example, we use the ToList method to get data:

C#
@Html.DevExpress().GridView(settings => { // ... settings.CallbackRouteValues = new { Controller = "EFGridMode", Action = "GridViewPartial" }; }).Bind(Model).GetHtml()
C#
NorthwindContext db = new NorthwindContext(); public ActionResult GridViewPartial() { return PartialView(db.Orders.ToList()); }

Database server mode

In database server mode, all data shaping operations are performed on the database server side. This mode is aimed at maintaining a large amount of data, for instance, 100k records. The GridView loads records on demand and performs data-aware operations (sorting, filtering, grouping, etc.) on the data server. This approach significantly improves the GridView’s speed and responsiveness.

Call a BindToEF method to bind the grid to the Entity Framework's data content. This method uses database server mode to optimize the execution of all queries to the data context initiated by the GridView.

C#
@Html.DevExpress().GridView(settings => { // ... settings.CallbackRouteValues = new { Controller = "EFDatabaseServerMode", Action = "GridViewPartial" }; }).BindToEF(string.Empty, string.Empty, (s, e) => { e.KeyExpression = "OrderID"; e.QueryableSource = Model; }).GetHtml()
C#
NorthwindContext db = new NorthwindContext(); public ActionResult GridViewPartial() { return PartialView(db.Orders); }

In this example, the grid is bound to the Northwind SQL Compact demo database data Model. Since table column names contain spaces, it is necessary to define the corresponding mappings at the DBContext level (see the OnModelCreating method implementation). In most cases, this step is not required.

Files to Review

Documentation

Does this example address your development requirements/objectives?

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

Example Code

Views/EFGridMode/GridViewPartial.cshtml
Razor
@Html.DevExpress().GridView(settings => { settings.Name = "grid"; settings.CallbackRouteValues = new { Controller = "EFGridMode", Action = "GridViewPartial" }; settings.KeyFieldName = "OrderID"; settings.Columns.Add("OrderID"); settings.Columns.Add("CustomerID"); settings.Columns.Add("ShipName"); settings.Columns.Add("ShipAddress"); settings.Settings.ShowGroupPanel = true; }).Bind(Model).GetHtml()
Views/EFDatabaseServerMode/GridViewPartial.cshtml
Razor
@Html.DevExpress().GridView(settings => { settings.Name = "grid"; settings.CallbackRouteValues = new { Controller = "EFDatabaseServerMode", Action = "GridViewPartial" }; settings.KeyFieldName = "OrderID"; settings.Columns.Add("OrderID"); settings.Columns.Add("CustomerID"); settings.Columns.Add("ShipName"); settings.Columns.Add("ShipAddress"); settings.Settings.ShowGroupPanel = true; }).BindToEF(string.Empty, string.Empty, (s, e) => { e.KeyExpression = "OrderID"; e.QueryableSource = Model; }).GetHtml()
Controllers/EFGridModeController.cs
C#
using System; using System.Linq; using System.Web.Mvc; using EF.Models; namespace EF.Controllers { public class EFGridModeController : Controller { NorthwindContext db = new NorthwindContext(); public ActionResult Index() { return View(); } public ActionResult GridViewPartial() { return PartialView(db.Orders.ToList()); } } }
Controllers/EFDatabaseServerModeController.cs
C#
using System; using System.Linq; using System.Web.Mvc; using EF.Models; namespace EF.Controllers { public class EFDatabaseServerModeController : Controller { NorthwindContext db = new NorthwindContext(); public ActionResult Index() { return View(); } public ActionResult GridViewPartial() { return PartialView(db.Orders); } } }
Models/Model.cs
C#
using System; using System.Data.Entity; using System.ComponentModel.DataAnnotations; namespace EF.Models { public class Order { [Key] public int OrderID { get; set; } public string CustomerID { get; set; } public string ShipName { get; set; } public string ShipAddress { get; set; } } public class NorthwindContext : DbContext { public NorthwindContext() : base("SQLCompact_Northwind_Connection") { } public DbSet<Order> Orders { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Order>().Property(p => p.OrderID).HasColumnName("Order ID"); modelBuilder.Entity<Order>().Property(p => p.CustomerID).HasColumnName("Customer ID"); modelBuilder.Entity<Order>().Property(p => p.ShipName).HasColumnName("Ship Name"); modelBuilder.Entity<Order>().Property(p => p.ShipAddress).HasColumnName("Ship Address"); } } }

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.