Example E3076
Visible to All Users

Grid View for ASP.NET Web Forms - How to Delete Selected Rows in a Grid

The example shows how to delete the selected rows of an ASPxGridView that is bound to an in-memory DataSource.

A grid with selected rows to be deleted

Use the client-side PerformCallback method to send custom callbacks to the server when a user clicks the Delete button.

ASPx
function OnClickButtonDel(s, e) { grid.PerformCallback('Delete'); } ... <dx:ASPxButton ID="buttonDel" AutoPostBack="false" runat="server" Text="Delete"> <ClientSideEvents Click="OnClickButtonDel"/> </dx:ASPxButton>

On the server, the PerformCallback method raises the CustomCallback event.

In the CustomCallback event handler, call the GetSelectedFieldValues method to obtain the selected rows. Then, call the Remove method for each selected row.

C#
protected void gridView_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e) { if(e.Parameters == "Delete") { table = GetTable(); List<Object> selectItems = grid.GetSelectedFieldValues("ID"); foreach(object selectItemId in selectItems) { table.Rows.Remove(table.Rows.Find(selectItemId)); } grid.DataBind(); grid.Selection.UnselectAll(); } }

Files to Look At

Documentation

More Examples

Example Code

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"> <!--region #Markup--> <head runat="server"> <script type="text/javascript"> function OnClickButtonDel(s, e) { grid.PerformCallback('Delete'); } </script> <title></title> </head> <body> <form id="form1" runat="server"> <div> <dx:ASPxGridView ID="grid" KeyFieldName="ID" runat="server" AutoGenerateColumns="False" OnCellEditorInitialize="gridView_CellEditorInitialize" OnRowInserting="gridView_RowInserting" OnRowUpdating="gridView_RowUpdating" ClientInstanceName="grid" OnCustomCallback="gridView_CustomCallback" ondatabinding="grid_DataBinding" > <Columns> <dx:GridViewCommandColumn VisibleIndex="0" ShowSelectCheckbox="True" ShowEditButton="true" ShowNewButton="true" ShowUpdateButton="true"> <FooterTemplate> <dx:ASPxButton ID="buttonDel" AutoPostBack="false" runat="server" Text="Delete"> <ClientSideEvents Click="OnClickButtonDel" /> </dx:ASPxButton> </FooterTemplate> </dx:GridViewCommandColumn> <dx:GridViewDataTextColumn FieldName="ID" ReadOnly="True" VisibleIndex="1"> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn FieldName="Data" VisibleIndex="2"> </dx:GridViewDataTextColumn> </Columns> <Settings ShowFooter="True" /> </dx:ASPxGridView> </div> </form> </body> <!--endregion #Markup--> </html>
Default.aspx.cs(vb)
C#
using System; using System.Data; using DevExpress.Web; using System.Collections.Generic; public partial class _Default : System.Web.UI.Page { DataTable table = null; protected void Page_Init(object sender, EventArgs e) { } protected void Page_Load(object sender, EventArgs e) { if(!IsCallback && !IsPostBack) grid.DataBind(); } protected void grid_DataBinding(object sender, EventArgs e) { grid.DataSource = GetTable(); } protected void gridView_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) { table = GetTable(); ASPxGridView gridView = (ASPxGridView)sender; DataRow row = table.NewRow(); row["ID"] = e.NewValues["ID"]; row["Data"] = e.NewValues["Data"]; gridView.CancelEdit(); e.Cancel = true; table.Rows.Add(row); } protected void gridView_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) { table = GetTable(); DataRow row = table.Rows.Find(e.Keys[0]); row["Data"] = e.NewValues["Data"]; grid.CancelEdit(); e.Cancel = true; } protected void gridView_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e) { ASPxGridView grid = (ASPxGridView)sender; if(e.Column.FieldName == "ID") { ASPxTextBox textBox = (ASPxTextBox)e.Editor; textBox.ClientEnabled = false; if(grid.IsNewRowEditing) { table = GetTable(); textBox.Text = GetNewId().ToString(); } } } #region #CustomCallbackMethod protected void gridView_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e) { if(e.Parameters == "Delete") { table = GetTable(); List<Object> selectItems = grid.GetSelectedFieldValues("ID"); foreach(object selectItemId in selectItems) { table.Rows.Remove(table.Rows.Find(selectItemId)); } grid.DataBind(); grid.Selection.UnselectAll(); } } #endregion #CustomCallbackMethod private int GetNewId() { table = GetTable(); if(table.Rows.Count == 0) return 0; int max = Convert.ToInt32(table.Rows[0]["ID"]); for(int i = 1; i < table.Rows.Count; i++) { if(Convert.ToInt32(table.Rows[i]["ID"]) > max) max = Convert.ToInt32(table.Rows[i]["ID"]); } return max + 1; } private DataTable GetTable() { DataTable table; if(Session["table"] == null) { table = new DataTable(); table.Columns.Add("ID", typeof(int)); table.Columns.Add("Data", typeof(string)); table.PrimaryKey = new DataColumn[] { table.Columns["ID"] }; for(int i = 0; i < 10; i++) { table.Rows.Add(new object[] { i, "row " + i.ToString() }); } Session["table"] = table; } else { table = (DataTable)Session["table"]; } return table; } }

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.