Grid View for ASP.NET Web Forms - How to implement common scenarios within a grid bound with Entity Framework 6
This example demonstrates how to bind ASPxGridView with Entity Framework 6 and implement common scenarios: CRUD operations, a master-detail grid, a lookup column, and server mode.
Follow the steps below to enable CRUD operations in ASPxGridView.
- Handle the RowInserting event.
- Create a
DataContext
instance. - Create a new data item and fill its properties from the e.NewValues dictionary.
- Add a new data item to the corresponding table.
- Save changes.
- Set the e.Cancel property to
true
to cancel the operation. - Call the CancelEdit method to close the edit form.
<dx:ASPxGridView ID="grid" runat="server" KeyFieldName="ProductID" OnRowInserting="grid_RowInserting" ...>
public Model1 DataContext { ... }
protected void grid_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) {
var product = new Product();
product.ProductName = (string)e.NewValues["ProductName"];
product.UnitPrice = (decimal)e.NewValues["UnitPrice"];
DataContext.Products.Add(product);
DataContext.SaveChanges();
e.Cancel = true;
grid.CancelEdit();
}
- Handle the RowUpdating event.
- Create a
DataContext
instance. - Use the e.Keys property to get the processed data item.
- Edit data item properties.
- Save changes.
- Set the e.Cancel property to
true
to cancel the operation. - Call the CancelEdit method to close the edit form.
<dx:ASPxGridView ID="grid" runat="server" KeyFieldName="ProductID" OnRowUpdating="grid_RowUpdating" ...>
public Model1 DataContext { ... }
protected void grid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) {
int productID = (int)e.Keys[grid.KeyFieldName];
Product product = DataContext.Products.Where(p => p.ProductID == productID).FirstOrDefault();
product.ProductName = (string)e.NewValues["ProductName"];
product.UnitPrice = (decimal)e.NewValues["UnitPrice"];
DataContext.SaveChanges();
e.Cancel = true;
grid.CancelEdit();
}
- Handle the RowDeleting event.
- Create a
DataContext
instance. - Use the e.Keys property to get the processed data item.
- Remove the data item.
- Save changes.
- Set the e.Cancel property to
true
to cancel the operation.
<dx:ASPxGridView ID="grid" runat="server" KeyFieldName="ProductID" OnRowDeleting="grid_RowDeleting" ...>
public Model1 DataContext { ... }
protected void grid_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e) {
int productID = (int)e.Keys[grid.KeyFieldName];
Product product = DataContext.Products.Where(p => p.ProductID == productID).FirstOrDefault();
DataContext.Products.Remove(product);
DataContext.SaveChanges();
e.Cancel = true;
}
- CustomEditingCapabilities.aspx (VB: CustomEditingCapabilities.aspx)
- CustomEditingCapabilities.aspx.cs (VB: CustomEditingCapabilities.aspx.vb)
- Place an
ASPxGridView
control in the DetailRow grid template to create a master-detail grid layout. - Handle the detail grid's
Init
event to bind the grid. Call the GetMasterRowKeyValue method to get the current master row's key value.
<dx:ASPxGridView ID="gridMaster" runat="server" AutoGenerateColumns="False"
OnDataBinding="gridMaster_DataBinding" KeyFieldName="CategoryID">
<Columns> ... </Columns>
<Templates>
<DetailRow>
<dx:ASPxGridView ID="gridDetail" runat="server"
AutoGenerateColumns="False" OnInit="gridDetail_Init" KeyFieldName="ProductID">
<Columns>
<dx:GridViewDataTextColumn FieldName="ProductID" >
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="ProductName" />
<dx:GridViewDataTextColumn FieldName="UnitPrice" />
<dx:GridViewDataCheckColumn FieldName="Discontinued" />
</Columns>
</dx:ASPxGridView>
</DetailRow>
</Templates>
<SettingsDetail ShowDetailRow="true" />
</dx:ASPxGridView>
protected void gridMaster_DataBinding(object sender, EventArgs e) {
gridMaster.ForceDataRowType(typeof(Category));
gridMaster.DataSource = DataContext.Categories.ToList();
}
protected void gridDetail_Init(object sender, EventArgs e) {
ASPxGridView grid = sender as ASPxGridView;
int masterKey = (int)grid.GetMasterRowKeyValue();
grid.DataSource = DataContext.Products.Where(p => p.CategoryID == masterKey).ToList();
grid.DataBind();
}
Add a column of the GridViewDataComboBoxColumn type in the Columns collection to create a lookup column in the grid. Bind the column to a data source in the Page_Init
event.
<dx:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="False" KeyFieldName="ProductID" ...>
<Columns>
...
<dx:GridViewDataComboBoxColumn Caption="Category" FieldName="CategoryID" >
<PropertiesComboBox TextField="CategoryName" ValueField="CategoryID" ValueType="System.Int32" />
</dx:GridViewDataComboBoxColumn>
</Columns>
</dx:ASPxGridView>
protected void Page_Init(object sender, EventArgs e) {
((GridViewDataComboBoxColumn)grid.DataColumns["CategoryID"]).PropertiesComboBox.DataSource = DataContext.Categories.ToList();
}
Use the EntityServerModeDataSource component to bind the ASPxGridView
control to a data source with the Entity Framework, and enable database server mode.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ServerModeEF.aspx.cs" Inherits="GridEntityFramework.ServerModeEF" %>
<dx:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="False" KeyFieldName="ProductID" OnDataBinding="grid_DataBinding">
<Columns> ... </Columns>
</dx:ASPxGridView>
protected void grid_DataBinding(object sender, EventArgs e) {
(sender as ASPxGridView).DataSource = GetEntityServerModeSource();
}
private EntityServerModeSource GetEntityServerModeSource() {
EntityServerModeSource esms = new EntityServerModeSource();
esms.QueryableSource = DataContext.Products;
esms.KeyExpression = "ProductID";
return esms;
}
- CustomEditingCapabilities.aspx (VB: CustomEditingCapabilities.aspx)
- CustomEditingCapabilities.aspx.cs (VB: CustomEditingCapabilities.aspx.vb)
- LookUpColumn.aspx (VB: LookUpColumn.aspx)
- LookUpColumn.aspx.cs (VB: LookUpColumn.aspx.vb)
- MasterDetail.aspx (VB: MasterDetail.aspx)
- MasterDetail.aspx.cs (VB: MasterDetail.aspx.vb)
- ServerModeEF.aspx (VB: ServerModeEF.aspx)
- ServerModeEF.aspx.cs (VB: ServerModeEF.aspx.vb)
(you will be redirected to DevExpress.com to submit your response)