ASPxGridView.CancelEdit() Method
Cancels all the changes made and switches the ASPxGridView to browse mode.
Namespace: DevExpress.Web
Assembly: DevExpress.Web.v19.1.dll
Declaration
End-users can do this by clicking the Cancel command item displayed within the Edit Form or edited row if in-line editing is used.
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.
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.FindVisibleIndexByKeyValue(e.Keys[ASPxGridView1.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;
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web" TagPrefix="dxwgv" %>
<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web" TagPrefix="dxe" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False"
KeyFieldName="ID" OnRowUpdating="ASPxGridView1_RowUpdating" Width="588px" OnRowDeleting="ASPxGridView1_RowDeleting" OnRowInserting="ASPxGridView1_RowInserting">
<Columns>
<dxwgv:GridViewCommandColumn VisibleIndex="0" ShowEditButton="True" ShowDeleteButton="True" ShowNewButton="True"/>
<dxwgv:GridViewDataTextColumn FieldName="ID" ReadOnly="True" VisibleIndex="1">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="Data" VisibleIndex="2">
</dxwgv:GridViewDataTextColumn>
</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 VisibleIndex="0" ShowEditButton="True"/>
<dxwgv:GridViewDataTextColumn FieldName="ID" ReadOnly="True" VisibleIndex="1">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="MasterID" ReadOnly="True" VisibleIndex="2">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="Data" VisibleIndex="3">
</dxwgv:GridViewDataTextColumn>
</Columns>
</dxwgv:ASPxGridView>
</DetailRow>
</Templates>
</dxwgv:ASPxGridView>
</div>
</form>
</body>
</html>
<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web" TagPrefix="dxwgv" %>
<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web" TagPrefix="dxe" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False"
KeyFieldName="ID" OnRowUpdating="ASPxGridView1_RowUpdating" Width="588px" OnRowDeleting="ASPxGridView1_RowDeleting" OnRowInserting="ASPxGridView1_RowInserting">
<Columns>
<dxwgv:GridViewCommandColumn VisibleIndex="0" ShowEditButton="True" ShowDeleteButton="True" ShowNewButton="True"/>
<dxwgv:GridViewDataTextColumn FieldName="ID" ReadOnly="True" VisibleIndex="1">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="Data" VisibleIndex="2">
</dxwgv:GridViewDataTextColumn>
</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 VisibleIndex="0" ShowEditButton="True"/>
<dxwgv:GridViewDataTextColumn FieldName="ID" ReadOnly="True" VisibleIndex="1">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="MasterID" ReadOnly="True" VisibleIndex="2">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="Data" VisibleIndex="3">
</dxwgv:GridViewDataTextColumn>
</Columns>
</dxwgv:ASPxGridView>
</DetailRow>
</Templates>
</dxwgv:ASPxGridView>
</div>
</form>
</body>
</html>
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.FindVisibleIndexByKeyValue(e.Keys(ASPxGridView1.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 snippets (auto-collected from DevExpress Examples) contain references to the CancelEdit() method.
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