Example E2945
Visible to All Users

Grid View for ASP.NET Web Forms - How to edit a data table stored in a ViewState

This example demonstrates how to create a data table and grid control at runtime and use the grid's server-side events to update the data table.

Edit a data table stored in ViewState

Overview

Create a data table at runtime and save it to a ViewState.

C#
private DataTable CustomDataSourse { get { if (dataTable != null) return dataTable; dataTable = ViewState["CustomTable"] as DataTable; if (dataTable != null) return dataTable; dataTable = new DataTable("CustomDTable"); dataTable.Columns.Add("Id", typeof(Int32)); dataTable.PrimaryKey = new DataColumn[] { dataTable.Columns[0] }; dataTable.Columns.Add("Data", typeof(string)); // ... ViewState["CustomTable"] = dataTable; return dataTable; } }

Create a grid control at runtime and set its EnableCallbacks property to false to enable postback mode. This mode allows you to store data in a ViewState.

C#
private void CreateGrid() { ASPxGridView grid = new ASPxGridView(); grid.ID = "grid"; this.Form.Controls.Add(grid); grid.EnableCallBacks = false; grid.KeyFieldName = "Id"; // ... }

Update the data table in the corresponding grid's event handlers and bind the grid to the updated data table.

C#
private void UpdateData(ASPxGridView g) { ViewState["CustomTable"] = dataTable; g.DataBind(); }

Files to Review

Documentation

More Examples

Example Code

WebSite/Default.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.15.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title> How to insert, edit and delete data in a data table by ASPxGridView at runtime</title> </head> <body> <form id="form1" runat="server"> <div></div> </form> </body> </html>
WebSite/Default.aspx.cs(vb)
C#
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using DevExpress.Web; using DevExpress.Web.Data; public partial class _Default : System.Web.UI.Page { private DataTable dataTable; protected void Page_Load(object sender, EventArgs e) { CreateGrid(); } private DataTable CustomDataSourse { get { if (dataTable != null) return dataTable; dataTable = ViewState["CustomTable"] as DataTable; if (dataTable != null) return dataTable; dataTable = new DataTable("CustomDTable"); dataTable.Columns.Add("Id", typeof(Int32)); dataTable.PrimaryKey = new DataColumn[] { dataTable.Columns[0] }; dataTable.Columns.Add("Data", typeof(string)); dataTable.Rows.Add(0, "Data1"); dataTable.Rows.Add(1, "Data2"); dataTable.Rows.Add(2, "Data3"); dataTable.Rows.Add(3, "Data4"); dataTable.Rows.Add(4, "Data5"); ViewState["CustomTable"] = dataTable; return dataTable; } } protected void grid_DataBinding(object sender, EventArgs e) { (sender as ASPxGridView).DataSource = CustomDataSourse; } protected void grid_DataBound(object sender, EventArgs e) { ASPxGridView g = sender as ASPxGridView; for (int i = 0; i < g.Columns.Count; i++) { GridViewDataTextColumn c = g.Columns[i] as GridViewDataTextColumn; if (c == null) continue; c.PropertiesTextEdit.ValidationSettings.RequiredField.IsRequired = true; } } protected void grid_RowDeleting(object sender, ASPxDataDeletingEventArgs e) { int id = (int)e.Keys[0]; DataRow dr = CustomDataSourse.Rows.Find(id); dataTable.Rows.Remove(dr); ASPxGridView g = sender as ASPxGridView; UpdateData(g); e.Cancel = true; } protected void grid_RowUpdating(object sender, ASPxDataUpdatingEventArgs e) { int id = (int)e.OldValues["Id"]; DataRow dr = CustomDataSourse.Rows.Find(id); dr[0] = e.NewValues["Id"]; dr[1] = e.NewValues["Data"]; ASPxGridView g = sender as ASPxGridView; UpdateData(g); g.CancelEdit(); e.Cancel = true; } protected void grid_RowInserting(object sender, ASPxDataInsertingEventArgs e) { CustomDataSourse.Rows.Add(e.NewValues["Id"], e.NewValues["Data"]); ASPxGridView g = sender as ASPxGridView; UpdateData(g); g.CancelEdit(); e.Cancel = true; } private void CreateGrid() { ASPxGridView grid = new ASPxGridView(); grid.ID = "grid"; this.Form.Controls.Add(grid); grid.EnableCallBacks = false; grid.KeyFieldName = "Id"; grid.DataBinding += grid_DataBinding; grid.RowDeleting += grid_RowDeleting; grid.RowUpdating += grid_RowUpdating; grid.RowInserting += grid_RowInserting; grid.DataBound += grid_DataBound; grid.RowValidating += new ASPxDataValidationEventHandler(grid_RowValidating); grid.DataBind(); if (!this.IsPostBack) { GridViewCommandColumn c = new GridViewCommandColumn(); grid.Columns.Add(c); c.ShowEditButton = true; c.ShowUpdateButton = true; c.ShowNewButtonInHeader = true; } GridViewDataTextColumn col = grid.Columns["Id"] as GridViewDataTextColumn; col.PropertiesTextEdit.ValidationSettings.RegularExpression.ValidationExpression = "\\d{1,9}"; } void grid_RowValidating(object sender, ASPxDataValidationEventArgs e) { int id = (int)e.NewValues["Id"]; if ((!e.OldValues.Contains("Id") || ((int)e.OldValues["Id"] != id)) && (CustomDataSourse.Rows.Find(id) != null)) { ASPxGridView grid = sender as ASPxGridView; e.Errors[grid.Columns["Id"]] = String.Format("Column 'Id' is constrained to be unique. Value '{0}' is already present.", id); } } private void UpdateData(ASPxGridView g) { ViewState["CustomTable"] = dataTable; g.DataBind(); } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.