Skip to main content
A newer version of this page is available. .
Tab

ASPxGridView.RowInserting Event

Enables you to cancel adding a new row.

Namespace: DevExpress.Web

Assembly: DevExpress.Web.v18.2.dll

Declaration

public event ASPxDataInsertingEventHandler RowInserting

Event Data

The RowInserting event's data class is ASPxDataInsertingEventArgs. 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.
NewValues Gets a dictionary that contains the new field name/value pairs for the row to be inserted.

Remarks

The RowInserting event occurs when an end-user tries to save the newly created row by clicking the Update command. To cancel the insert operation, set the event parameter’s Cancel property to true.

To create a new row, click the New command or call the ASPxGridView.AddNewRow method.

After a new row has been added to the ASPxGridView, the ASPxGridView.RowInserted 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.
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;
    }
}

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the RowInserting 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