Skip to main content

GridViewExtension.BindToEF(String, String, EventHandler<LinqServerModeDataSourceSelectEventArgs>, EventHandler<ServerModeExceptionThrownEventArgs>) Method

Binds the GridView to a data source via the Entity Framework in database server mode.

Namespace: DevExpress.Web.Mvc

Assembly: DevExpress.Web.Mvc5.v24.2.dll

Declaration

public GridViewExtension BindToEF(
    string contextTypeName,
    string tableName,
    EventHandler<LinqServerModeDataSourceSelectEventArgs> selectingMethod,
    EventHandler<ServerModeExceptionThrownEventArgs> exceptionThrownMethod
)

Parameters

Name Type Description
contextTypeName String

A String object representing the DataContext type name.

tableName String

A String object specifying the table name.

selectingMethod EventHandler<LinqServerModeDataSourceSelectEventArgs>

A method to which selection logic is delegated.

exceptionThrownMethod EventHandler<ServerModeExceptionThrownEventArgs>

A delegate method that allows you to catch unhandled/CLR exceptions.

Returns

Type Description
GridViewExtension

A GridViewExtension object representing the GridView extension.

Remarks

Using the BindToEF method, you can easily bind the GridView to Entity Framework data models. Simply call the method, pass the DataContext and table name as method parameters, and specify the key field via the GridView’s GridSettingsBase.KeyFieldName property.

Internally, the BindToEF method uses our EntityServerModeDataSource component, which was specifically designed to allow the GridView to efficiently process large amounts of data. The EntityServerModeDataSource component automatically enables database server mode to optimize the execution of all queries to the data context initiated by the GridView. In this mode, the GridView loads records on demand and performs data-aware operations (sorting, filtering, grouping, etc.) on the data server. This technique significantly improves the GridView’s speed and responsiveness.

Example

The code sample below demonstrates how to catch the internal exception thrown when using the Database Server Mode data binding approach.

View code (Razor):

@Html.DevExpress().GridView(settings => {
    // GridView settings
}).BindToEF(string.Empty, string.Empty, (s, e) => {
    // Specify queryable source
    var dataContext = new MyProject.Models.LargeDatabaseDataContext();
    e.QueryableSource = dataContext.Emails;
    e.KeyExpression = "Id";
}, (s, e) => {
    // Handle internal exception
    ViewContext.Writer.Write(e.Exception.Message + Environment.NewLine + e.Exception.StackTrace);
}).GetHtml()
See Also