Skip to main content
Tab

ASPxGridView.RowDeleting Event

Enables you to prevent a row from being deleted.

Namespace: DevExpress.Web

Assembly: DevExpress.Web.v24.2.dll

Declaration

public event ASPxDataDeletingEventHandler RowDeleting

Event Data

The RowDeleting event's data class is ASPxDataDeletingEventArgs. The following properties provide information specific to this event:

Property Description
Cancel Gets or sets a value indicating whether the event should be canceled. Inherited from CancelEventArgs.
Keys Gets a dictionary of field name/value pairs that represent the primary key of the row to delete.
Values Gets a dictionary of the non-key field name/value pairs for the row to delete.

Remarks

The RowDeleting event occurs when an end-user has clicked the Delete command or the ASPxGridView.DeleteRow method has been called. To cancel the delete operation, set the event parameter’s Cancel property to true.

After a row has been deleted, the ASPxGridView.RowDeleted event is raised.

Example

This demo shows how to edit data from an in-memory dataset using the ASPxGridView. To do this, you should:

  • handle the RowUpdating, RowInserting, and RowDeleting events and update the data source manually (the e.NewValues dictionary contains the input value);
  • set the e.Cancel parameter to true and call the ASPxGridView.CancelEdit method.

View Example

<dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False"
    KeyFieldName="ID" OnRowUpdating="ASPxGridView1_RowUpdating" Width="588px" OnRowDeleting="ASPxGridView1_RowDeleting" OnRowInserting="ASPxGridView1_RowInserting">
    <Columns>
        <dxwgv:GridViewCommandColumn ShowEditButton="True" ShowDeleteButton="True" ShowNewButton="True"/>
        <dxwgv:GridViewDataTextColumn FieldName="ID" ReadOnly="True" />
        <dxwgv:GridViewDataTextColumn FieldName="Data" />
    </Columns>
    <SettingsDetail ShowDetailRow="True" />
    <Templates>
        <DetailRow>
            <dxwgv:ASPxGridView ID="ASPxGridView2" runat="server" AutoGenerateColumns="False"
                KeyFieldName="ID" OnBeforePerformDataSelect="ASPxGridView2_BeforePerformDataSelect"
                OnRowUpdating="ASPxGridView1_RowUpdating" Width="100%">
                <Columns>
                    <dxwgv:GridViewCommandColumn ShowEditButton="True"/>
                    <dxwgv:GridViewDataTextColumn FieldName="ID" ReadOnly="True" />
                    <dxwgv:GridViewDataTextColumn FieldName="MasterID" ReadOnly="True" />
                    <dxwgv:GridViewDataTextColumn FieldName="Data" />
                </Columns>
            </dxwgv:ASPxGridView>
        </DetailRow>
    </Templates>
</dxwgv:ASPxGridView>
using System;
using System.Data;
using System.Configuration;
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 System.Collections;

public partial class _Default : System.Web.UI.Page 
{
    DataSet ds = null;
    protected void Page_Init(object sender, EventArgs e) {
        if (!IsPostBack || (Session["DataSet"] == null)) {
            ds = new DataSet();
            DataTable masterTable = new DataTable();
            masterTable.Columns.Add("ID", typeof(int));
            masterTable.Columns.Add("Data", typeof(string));
            masterTable.PrimaryKey = new DataColumn[] { masterTable.Columns["ID"] };

            DataTable detailTable = new DataTable();
            detailTable.Columns.Add("ID", typeof(int));
            detailTable.Columns.Add("MasterID", typeof(int));
            detailTable.Columns.Add("Data", typeof(string));
            detailTable.PrimaryKey = new DataColumn[] { detailTable.Columns["ID"] };
            int index = 0;
            for(int i = 0;i < 20;i++) {
                masterTable.Rows.Add(new object[] { i, "Master Row " + i });
                for(int j = 0;j < 5;j++)
                    detailTable.Rows.Add(new object[] { index++, i, "Detail Row " + j });
            }
            ds.Tables.AddRange(new DataTable[] { masterTable, detailTable });
            Session["DataSet"] = ds;
        }
        else
            ds = (DataSet)Session["DataSet"];
        ASPxGridView1.DataSource = ds.Tables[0];
        ASPxGridView1.DataBind();
    }
    protected void ASPxGridView2_BeforePerformDataSelect(object sender, EventArgs e) {
        ds = (DataSet)Session["DataSet"];
        DataTable detailTable = ds.Tables[1];
        DataView dv = new DataView(detailTable);
        ASPxGridView detailGridView = (ASPxGridView)sender;
        dv.RowFilter = "MasterID = " + detailGridView.GetMasterRowKeyValue();
        detailGridView.DataSource = dv;
    }
    protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) {
        ds = (DataSet)Session["DataSet"];
        ASPxGridView gridView = (ASPxGridView)sender;
        DataTable dataTable = gridView.GetMasterRowKeyValue() != null ? ds.Tables[1] : ds.Tables[0];
        DataRow row = dataTable.Rows.Find(e.Keys[0]);
        IDictionaryEnumerator enumerator = e.NewValues.GetEnumerator();
        enumerator.Reset();
        while(enumerator.MoveNext())
            row[enumerator.Key.ToString()] = enumerator.Value;
        gridView.CancelEdit();
        e.Cancel = true;
    }
    protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) {
        ds = (DataSet)Session["DataSet"];
        ASPxGridView gridView = (ASPxGridView)sender;
        DataTable dataTable = gridView.GetMasterRowKeyValue() != null ? ds.Tables[1] : ds.Tables[0];
        DataRow row = dataTable.NewRow();
        e.NewValues["ID"] = GetNewId();
        IDictionaryEnumerator enumerator = e.NewValues.GetEnumerator();
        enumerator.Reset();
        while(enumerator.MoveNext())
            if(enumerator.Key.ToString() != "Count")
                row[enumerator.Key.ToString()] = enumerator.Value;
        gridView.CancelEdit();
        e.Cancel = true;
        dataTable.Rows.Add(row);
    }

    protected void ASPxGridView1_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e) {
        int i = ASPxGridView1.FindSPxGridView1.KeyFieldName]);
        Control c = ASPxGridView1.FindDetailRowTemplateControl(i, "ASPxGridView2");
        e.Cancel = true;
        ds = (DataSet)Session["DataSet"];
        ds.Tables[0].Rows.Remove(ds.Tables[0].Rows.Find(e.Keys[ASPxGridView1.KeyFieldName]));

    }
    private int GetNewId() {
        ds = (DataSet)Session["DataSet"];
        DataTable table= ds.Tables[0];
        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;
    }
}

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the RowDeleting event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also