Example E5045
Visible to All Users

Grid View for ASP.NET Web Forms - A simple batch editing implementation

This example demonstrates how to use the ASPxGridView control's batch edit mode events to update a custom object data source stored in the session.

Simple Batch Edit implementation

When a Grid View is bound to an object data source at runtime, you need to implement the CRUD logic required to edit data. To do this, use one of the following options:

  • Handle the ASPxGridView's RowInserting, RowUpdating, and RowDeleting events.
    This technique allows you to handle each CRUD operation type in a separate event handler.
  • Handle the BatchUpdate event.
    Use this technique to process all operations in a single handler.

Files to Look At

Documentation

More Examples

Example Code

WebSite/Default.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register Assembly="DevExpress.Web.v18.2, Version=18.2.18.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dx" %> <!DOCTYPE html> <html> <head runat="server"> <title>ASPxGridView - A simple Batch Editing implementation</title> </head> <body> <form id="frmMain" runat="server"> <dx:ASPxCheckBox ID="BatchUpdateCheckBox" runat="server" Text="Handle BatchUpdate event" AutoPostBack="true" /> <dx:ASPxGridView ID="Grid" runat="server" KeyFieldName="ID" OnBatchUpdate="Grid_BatchUpdate" OnRowInserting="Grid_RowInserting" OnRowUpdating="Grid_RowUpdating" OnRowDeleting="Grid_RowDeleting"> <Columns> <dx:GridViewCommandColumn ShowNewButtonInHeader="true" ShowDeleteButton="true" /> <dx:GridViewDataColumn FieldName="C1" /> <dx:GridViewDataSpinEditColumn FieldName="C2" /> <dx:GridViewDataTextColumn FieldName="C3" /> <dx:GridViewDataCheckColumn FieldName="C4" /> <dx:GridViewDataDateColumn FieldName="C5" /> </Columns> <SettingsEditing Mode="Batch" /> </dx:ASPxGridView> </form> </body> </html>
WebSite/Default.aspx.cs(vb)
C#
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; using System.Linq; using DevExpress.Web.Data; public partial class _Default : System.Web.UI.Page { protected List<GridDataItem> GridData { get { var key = "34FAA431-CF79-4869-9488-93F6AAE81263"; if(!IsPostBack || Session[key] == null) Session[key] = Enumerable.Range(0, 100).Select(i => new GridDataItem { ID = i, C1 = i % 2, C2 = i * 0.5 % 3, C3 = "C3 " + i, C4 = i % 2 == 0, C5 = new DateTime(2013 + i, 12, 16) }).ToList(); return (List<GridDataItem>)Session[key]; } } protected void Page_Load(object sender, EventArgs e) { Grid.DataSource = GridData; Grid.DataBind(); } protected void Grid_RowInserting(object sender, ASPxDataInsertingEventArgs e) { InsertNewItem(e.NewValues); CancelEditing(e); } protected void Grid_RowUpdating(object sender, ASPxDataUpdatingEventArgs e) { UpdateItem(e.Keys, e.NewValues); CancelEditing(e); } protected void Grid_RowDeleting(object sender, ASPxDataDeletingEventArgs e) { DeleteItem(e.Keys, e.Values); CancelEditing(e); } protected void Grid_BatchUpdate(object sender, ASPxDataBatchUpdateEventArgs e) { if(!BatchUpdateCheckBox.Checked) return; foreach(var args in e.InsertValues) InsertNewItem(args.NewValues); foreach(var args in e.UpdateValues) UpdateItem(args.Keys, args.NewValues); foreach(var args in e.DeleteValues) DeleteItem(args.Keys, args.Values); e.Handled = true; } protected GridDataItem InsertNewItem(OrderedDictionary newValues) { var item = new GridDataItem() { ID = GridData.Count }; LoadNewValues(item, newValues); GridData.Add(item); return item; } protected GridDataItem UpdateItem(OrderedDictionary keys, OrderedDictionary newValues) { var id = Convert.ToInt32(keys["ID"]); var item = GridData.First(i => i.ID == id); LoadNewValues(item, newValues); return item; } protected GridDataItem DeleteItem(OrderedDictionary keys, OrderedDictionary values) { var id = Convert.ToInt32(keys["ID"]); var item = GridData.First(i => i.ID == id); GridData.Remove(item); return item; } protected void LoadNewValues(GridDataItem item, OrderedDictionary values) { item.C1 = Convert.ToInt32(values["C1"]); item.C2 = Convert.ToDouble(values["C2"]); item.C3 = Convert.ToString(values["C3"]); item.C4 = Convert.ToBoolean(values["C4"]); item.C5 = Convert.ToDateTime(values["C5"]); } protected void CancelEditing(CancelEventArgs e) { e.Cancel = true; Grid.CancelEdit(); } public class GridDataItem { public int ID { get; set; } public int C1 { get; set; } public double C2 { get; set; } public string C3 { get; set; } public bool C4 { get; set; } public DateTime C5 { get; set; } } }

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.