Back to Devexpress

How to: Implement Data Editing When a Grid is Bound to a Table Created At Runtime

aspnet-5336-components-grid-view-examples-how-to-implement-data-editing-when-a-grid-is-bound-to-a-table-created-at-runtime.md

latest5.2 KB
Original Source

How to: Implement Data Editing When a Grid is Bound to a Table Created At Runtime

  • Oct 04, 2021
  • 3 minutes to read

In this example, the ASPxGridView is bound to a DataTable created at runtime. The data table is stored within a Session. To enable data management, handle the following events:

Note

You should specify the ASPxGridBase.KeyFieldName property to perform data edit operations.

The animation below shows the result:

csharp
using System.Data;
...
protected void Page_Load(object sender, EventArgs e) {
    if (Session["GridData"] == null)
        Session["GridData"] = CreateData();

    ASPxGridView1.DataSource = Session["GridData"];

    if (!IsPostBack && !IsCallback)
        ASPxGridView1.DataBind();
}

private DataTable CreateData() {
    DataTable table = new DataTable();
    table.PrimaryKey = new DataColumn[] { table.Columns.Add("ID", typeof(Guid)) };
    table.Columns.Add("Person", typeof(string));
    table.Columns.Add("Age", typeof(int));
    table.Rows.Add(new object[] { Guid.NewGuid(), "Mike", 30 });
    table.Rows.Add(new object[] { Guid.NewGuid(), "Rosa", 22 });
    table.Rows.Add(new object[] { Guid.NewGuid(), "Nick", 18 });
    return table;
}
protected void ASPxGridView1_RowInserting(object sender,
    DevExpress.Web.Data.ASPxDataInsertingEventArgs e) {
    ((DataTable)Session["GridData"]).Rows.Add(new object[] { Guid.NewGuid(),
                            e.NewValues["Person"], e.NewValues["Age"] });
    e.Cancel = true;
    ASPxGridView1.CancelEdit();
    ASPxGridView1.DataBind();
}
protected void ASPxGridView1_RowUpdating(object sender, 
DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) {
    DataRow row = ((DataTable)Session["GridData"]).Rows.Find(e.Keys["ID"]);
    row["Person"] = e.NewValues["Person"];
    row["Age"] = e.NewValues["Age"];
    e.Cancel = true;
    ASPxGridView1.CancelEdit();
    ASPxGridView1.DataBind();
}
protected void ASPxGridView1_RowDeleting(object sender, 
DevExpress.Web.Data.ASPxDataDeletingEventArgs e) {
    DataRow row = ((DataTable)Session["GridData"]).Rows.Find(e.Keys["ID"]);
    ((DataTable)Session["GridData"]).Rows.Remove(row);
    e.Cancel = true;
    ASPxGridView1.DataBind();
}
vb
Imports System.Data
...
Protected Sub Page_Load(sender As Object, e As EventArgs)
    If Session("GridData") Is Nothing Then
        Session("GridData") = CreateData()
    End If

    ASPxGridView1.DataSource = Session("GridData")

    If Not IsPostBack AndAlso Not IsCallback Then
        ASPxGridView1.DataBind()
    End If
End Sub

Private Function CreateData() As DataTable
    Dim table As New DataTable()
    table.PrimaryKey = New DataColumn() {table.Columns.Add("ID", GetType(Guid))}
    table.Columns.Add("Person", GetType(String))
    table.Columns.Add("Age", GetType(Integer))
    table.Rows.Add(New Object() {Guid.NewGuid(), "Mike", 30})
    table.Rows.Add(New Object() {Guid.NewGuid(), "Rosa", 22})
    table.Rows.Add(New Object() {Guid.NewGuid(), "Nick", 18})
    Return table
End Function
Protected Sub ASPxGridView1_RowInserting(sender As Object, e As DevExpress.Web.Data.ASPxDataInsertingEventArgs)
    DirectCast(Session("GridData"), DataTable).Rows.Add(New Object() {Guid.NewGuid(), e.NewValues("Person"), e.NewValues("Age")})
    e.Cancel = True
    ASPxGridView1.CancelEdit()
    ASPxGridView1.DataBind()
End Sub
Protected Sub ASPxGridView1_RowUpdating(sender As Object, e As DevExpress.Web.Data.ASPxDataUpdatingEventArgs)
    Dim row As DataRow = DirectCast(Session("GridData"), DataTable).Rows.Find(e.Keys("ID"))
    row("Person") = e.NewValues("Person")
    row("Age") = e.NewValues("Age")
    e.Cancel = True
    ASPxGridView1.CancelEdit()
    ASPxGridView1.DataBind()
End Sub
Protected Sub ASPxGridView1_RowDeleting(sender As Object, e As DevExpress.Web.Data.ASPxDataDeletingEventArgs)
    Dim row As DataRow = DirectCast(Session("GridData"), DataTable).Rows.Find(e.Keys("ID"))
    DirectCast(Session("GridData"), DataTable).Rows.Remove(row)
    e.Cancel = True
    ASPxGridView1.DataBind()
End Sub
aspx
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" KeyFieldName="ID"
     OnRowDeleting="ASPxGridView1_RowDeleting" OnRowInserting="ASPxGridView1_RowInserting" 
     OnRowUpdating="ASPxGridView1_RowUpdating">
     <Columns>
          <dx:GridViewCommandColumn VisibleIndex="0" ShowDeleteButton="True" ShowEditButton="True" ShowNewButton="True">
          </dx:GridViewCommandColumn>
          <dx:GridViewDataTextColumn FieldName="ID" Visible="False" VisibleIndex="1">
          </dx:GridViewDataTextColumn>
          <dx:GridViewDataTextColumn FieldName="Person" VisibleIndex="2">
          </dx:GridViewDataTextColumn>
          <dx:GridViewDataSpinEditColumn FieldName="Age" VisibleIndex="3">
          </dx:GridViewDataSpinEditColumn>
     </Columns>
</dx:ASPxGridView>