Back to Devexpress

ASPxGridView.RowInserting Event

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

latest6.9 KB
Original Source

ASPxGridView.RowInserting Event

Enables you to cancel adding a new row.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public event ASPxDataInsertingEventHandler RowInserting
vb
Public Event RowInserting As ASPxDataInsertingEventHandler

Event Data

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

Remarks

The RowInserting event occurs when a user tries to save the newly inserted row by clicking the Update command. To cancel the insert operation, set the Cancel argument property to true.

View Example: How to use the Rich Text Editor to edit formatted text in the Edit Form

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

After a new row has been added to ASPxGridView, the RowInserted event is raised.

Example

This example demonstrates how to use ASPxGridView to edit an in-memory data set with a master-detail relationship.

View Example: How to edit an in-memory data set with a master-detail relationship

To enable this behavior, follow the steps below:

  1. Handle the RowInserting, RowUpdating, and RowDeleting events and update the data source manually.

  2. Set the cancel argument property to true and call the grid’s CancelEdit() method.

aspx
<dx:ASPxGridView ID="MasterGridView" runat="server" AutoGenerateColumns="False" SettingsPager-PageSize="3"
    KeyFieldName="ID" OnRowUpdating="MasterGridView_RowUpdating" Width="588px" OnRowDeleting="MasterGridView_RowDeleting" OnRowInserting="MasterGridView_RowInserting">
    <!-- ... -->
    <SettingsDetail ShowDetailRow="True" />
    <Templates>
        <DetailRow>
            <dx:ASPxGridView ID="DetailGridView" runat="server" AutoGenerateColumns="False" SettingsPager-PageSize="4"
                KeyFieldName="ID" OnBeforePerformDataSelect="DetailGridView_BeforePerformDataSelect"
                OnRowUpdating="MasterGridView_RowUpdating" Width="100%">
                <!-- ... -->
            </dx:ASPxGridView>
        </DetailRow>
    </Templates>
</dx:ASPxGridView>
csharp
protected void MasterGridView_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);
}
vb
Protected Sub MasterGridView_RowInserting(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataInsertingEventArgs)
    ds = CType(Session("DataSet"), DataSet)
    Dim gridView As ASPxGridView = CType(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()
    While enumerator.MoveNext()
        If Not Equals(enumerator.Key.ToString(), "Count") Then row(enumerator.Key.ToString()) = enumerator.Value
    End While

    gridView.CancelEdit()
    e.Cancel = True
    dataTable.Rows.Add(row)
End Sub

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.

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

csharp
detailGrid.DataBind();
detailGrid.RowInserting += new DevExpress.Web.Data.ASPxDataInsertingEventHandler(detailGrid_RowInserting);
detailGrid.RowUpdating += new DevExpress.Web.Data.ASPxDataUpdatingEventHandler(detailGrid_RowUpdating);

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

vb
detailGrid.DataBind()
AddHandler detailGrid.RowInserting, AddressOf detailGrid_RowInserting
AddHandler detailGrid.RowUpdating, AddressOf detailGrid_RowUpdating

See Also

ASPxGridView Class

ASPxGridView Members

DevExpress.Web Namespace