Back to Devexpress

ASPxGridView.RowDeleting Event

aspnet-devexpress-dot-web-dot-aspxgridview-b2737d17.md

latest14.7 KB
Original Source

ASPxGridView.RowDeleting Event

Enables you to prevent a row from being deleted.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public event ASPxDataDeletingEventHandler RowDeleting
vb
Public Event RowDeleting As ASPxDataDeletingEventHandler

Event Data

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

PropertyDescription
CancelGets or sets a value indicating whether the event should be canceled. Inherited from CancelEventArgs.
KeysGets a dictionary of field name/value pairs that represent the primary key of the row to delete.
ValuesGets 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

aspx
<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>
csharp
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;
    }
}
vb
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports DevExpress.Web
Imports System.Collections

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Private ds As DataSet = Nothing
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
        If (Not IsPostBack) OrElse (Session("DataSet") Is Nothing) Then
            ds = New DataSet()
            Dim masterTable As New DataTable()
            masterTable.Columns.Add("ID", GetType(Integer))
            masterTable.Columns.Add("Data", GetType(String))
            masterTable.PrimaryKey = New DataColumn() { masterTable.Columns("ID") }

            Dim detailTable As New DataTable()
            detailTable.Columns.Add("ID", GetType(Integer))
            detailTable.Columns.Add("MasterID", GetType(Integer))
            detailTable.Columns.Add("Data", GetType(String))
            detailTable.PrimaryKey = New DataColumn() { detailTable.Columns("ID") }
            Dim index As Integer = 0
            For i As Integer = 0 To 19
                masterTable.Rows.Add(New Object() { i, "Master Row " & i })
                For j As Integer = 0 To 4
                    detailTable.Rows.Add(New Object() {index, i, "Detail Row " & j})
                    index += 1
                Next j
            Next i
            ds.Tables.AddRange(New DataTable() { masterTable, detailTable })
            Session("DataSet") = ds
        Else
            ds = DirectCast(Session("DataSet"), DataSet)
        End If
        ASPxGridView1.DataSource = ds.Tables(0)
        ASPxGridView1.DataBind()
    End Sub
    Protected Sub ASPxGridView2_BeforePerformDataSelect(ByVal sender As Object, ByVal e As EventArgs)
        ds = DirectCast(Session("DataSet"), DataSet)
        Dim detailTable As DataTable = ds.Tables(1)
        Dim dv As New DataView(detailTable)
        Dim detailGridView As ASPxGridView = DirectCast(sender, ASPxGridView)
        dv.RowFilter = "MasterID = " & detailGridView.GetMasterRowKeyValue()
        detailGridView.DataSource = dv
    End Sub
    Protected Sub ASPxGridView1_RowUpdating(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataUpdatingEventArgs)
        ds = DirectCast(Session("DataSet"), DataSet)
        Dim gridView As ASPxGridView = DirectCast(sender, ASPxGridView)
        Dim dataTable As DataTable = If(gridView.GetMasterRowKeyValue() IsNot Nothing, ds.Tables(1), ds.Tables(0))
        Dim row As DataRow = dataTable.Rows.Find(e.Keys(0))
        Dim enumerator As IDictionaryEnumerator = e.NewValues.GetEnumerator()
        enumerator.Reset()
        Do While enumerator.MoveNext()
            row(enumerator.Key.ToString()) = enumerator.Value
        Loop
        gridView.CancelEdit()
        e.Cancel = True
    End Sub
    Protected Sub ASPxGridView1_RowInserting(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataInsertingEventArgs)
        ds = DirectCast(Session("DataSet"), DataSet)
        Dim gridView As ASPxGridView = DirectCast(sender, ASPxGridView)
        Dim dataTable As DataTable = If(gridView.GetMasterRowKeyValue() IsNot Nothing, ds.Tables(1), ds.Tables(0))
        Dim row As DataRow = dataTable.NewRow()
        e.NewValues("ID") = GetNewId()
        Dim enumerator As IDictionaryEnumerator = e.NewValues.GetEnumerator()
        enumerator.Reset()
        Do While enumerator.MoveNext()
            If enumerator.Key.ToString() <> "Count" Then
                row(enumerator.Key.ToString()) = enumerator.Value
            End If
        Loop
        gridView.CancelEdit()
        e.Cancel = True
        dataTable.Rows.Add(row)
    End Sub

    Protected Sub ASPxGridView1_RowDeleting(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataDeletingEventArgs)
        Dim i As Integer = ASPxGridView1.FindSPxGridView1.KeyFieldName))
        Dim c As Control = ASPxGridView1.FindDetailRowTemplateControl(i, "ASPxGridView2")
        e.Cancel = True
        ds = DirectCast(Session("DataSet"), DataSet)
        ds.Tables(0).Rows.Remove(ds.Tables(0).Rows.Find(e.Keys(ASPxGridView1.KeyFieldName)))

    End Sub
    Private Function GetNewId() As Integer
        ds = DirectCast(Session("DataSet"), DataSet)
        Dim table As DataTable= ds.Tables(0)
        If table.Rows.Count = 0 Then
            Return 0
        End If
        Dim max As Integer = Convert.ToInt32(table.Rows(0)("ID"))
        For i As Integer = 1 To table.Rows.Count - 1
            If Convert.ToInt32(table.Rows(i)("ID")) > max Then
                max = Convert.ToInt32(table.Rows(i)("ID"))
            End If
        Next i
        Return max + 1
    End Function
End Class

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.

asp-net-web-forms-grid-create-master-detail-grid-at-runtime/CS/MasterDetailGrids/Default.aspx.cs#L103

csharp
detailGrid.RowUpdating += new DevExpress.Web.Data.ASPxDataUpdatingEventHandler(detailGrid_RowUpdating);
    detailGrid.RowDeleting += new DevExpress.Web.Data.ASPxDataDeletingEventHandler(detailGrid_RowDeleting);
}

asp-net-web-forms-grid-create-master-detail-grid-at-runtime/VB/MasterDetailGrids/Default.aspx.vb#L107

vb
AddHandler detailGrid.RowUpdating, AddressOf detailGrid_RowUpdating
    AddHandler detailGrid.RowDeleting, AddressOf detailGrid_RowDeleting
End Sub

See Also

DeleteRow(Int32)

RowDeleted

Grid View

ASPxGridView Class

ASPxGridView Members

DevExpress.Web Namespace