Skip to content

DevExpress-Examples/asp-net-web-forms-grid-entitydatasource-and-common-scenarios

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Implementation Details

CRUD Operations

Follow the steps below to enable CRUD operations in ASPxGridView.

Insert a Row

  1. Handle the RowInserting event.
  2. Create a DataContext instance.
  3. Create a new data item and fill its properties from the e.NewValues dictionary.
  4. Add a new data item to the corresponding table.
  5. Save changes.
  6. Set the e.Cancel property to true to cancel the operation.
  7. 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();
}

Edit a Row

  1. Handle the RowUpdating event.
  2. Create a DataContext instance.
  3. Use the e.Keys property to get the processed data item.
  4. Edit data item properties.
  5. Save changes.
  6. Set the e.Cancel property to true to cancel the operation.
  7. 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();
}

Delete a Row

  1. Handle the RowDeleting event.
  2. Create a DataContext instance.
  3. Use the e.Keys property to get the processed data item.
  4. Remove the data item.
  5. Save changes.
  6. 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;
}

Files to Review

Master-Detail Layout

  1. Place an ASPxGridView control in the DetailRow grid template to create a master-detail grid layout.
  2. 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();
}

Files to Review

Lookup Column

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

}

Files to Review

Server Mode

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;
}

Files to Review

Files to Review

Documentation

More Examples

About

Bind ASPxGridView with EntityDataSource and implement CRUD operations, a master-detail grid, a LookUp column, and server mode.

Topics

Resources

License

Stars

Watchers

Forks