Skip to content

Explore two approaches on how to bind a grid to Entity Framework

License

Notifications You must be signed in to change notification settings

DevExpress-Examples/asp-net-mvc-grid-bind-to-entity-framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

@Html.DevExpress().GridView(settings => {
    // ...
    settings.CallbackRouteValues = new { Controller = "EFGridMode", Action = "GridViewPartial" };
}).Bind(Model).GetHtml()
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.

@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()
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)