Skip to main content
Tab

ASPxGridView.AddNewRow() Method

Adds a new record.

Namespace: DevExpress.Web

Assembly: DevExpress.Web.v24.1.dll

NuGet Package: DevExpress.Web

Declaration

public void AddNewRow()

Remarks

The AddNewRow method switches the ASPxGridView to edit mode and allows the values of the new row to be edited. To add the new record to the underlying datasource, call the ASPxGridView.UpdateEdit (or ASPxClientGridView.UpdateEdit) method. End-users can save the changes made using the Update command.

To initialize row values in code, handle the ASPxGridView.InitNewRow event.

Example

The following example shows how to make the form used for row editing always visible. To implement this, you should add some lines of code onto the form page.

<dx:ASPxGridView ID="gridView" runat="server" AutoGenerateColumns="False"
    DataSourceID="AccessDataSource1" KeyFieldName="CategoryID" OnBeforeGetCallbackResult="ASPxGridView1_BeforeGetCallbackResult" >
    <Columns>
        <dx:GridViewCommandColumn ShowEditButton="True" ShowNewButton="True"/>
        <dx:GridViewDataTextColumn FieldName="CategoryID" ReadOnly="True" >
            <EditFormSettings Visible="False" />
        </dx:GridViewDataTextColumn>
        <dx:GridViewDataTextColumn FieldName="CategoryName" />
        <dx:GridViewDataTextColumn FieldName="Description" />
    </Columns>
</dx:ASPxGridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/nwind.mdb"
    SelectCommand="SELECT * FROM [Categories]" DeleteCommand="DELETE FROM [Categories] WHERE [CategoryID] = ?"
    InsertCommand="INSERT INTO [Categories] ([CategoryID], [CategoryName], [Description]) VALUES (?, ?, ?)" 
    UpdateCommand="UPDATE [Categories] SET [CategoryName] = ?, [Description] = ? WHERE [CategoryID] = ?">
    <DeleteParameters>
        <asp:Parameter Name="CategoryID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="CategoryName" Type="String" />
        <asp:Parameter Name="Description" Type="String" />
        <asp:Parameter Name="CategoryID" Type="Int32" />
    </UpdateParameters>
    <InsertParameters>
        <asp:Parameter Name="CategoryID" Type="Int32" />
        <asp:Parameter Name="CategoryName" Type="String" />
        <asp:Parameter Name="Description" Type="String" />
    </InsertParameters>
</asp:AccessDataSource>
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;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            gridView.AddNewRow();
        }
        DisableUpdateInDemo();
    }
    protected void ASPxGridView1_BeforeGetCallbackResult(object sender, EventArgs e) {
        if (!gridView.IsEditing && !gridView.IsNewRowEditing) {
            gridView.AddNewRow();
        }
    }
    protected void gridView_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e) {

    }
    protected void gridView_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) {

    }


    //Database update is not allowed in online demos
    private void DisableUpdateInDemo() {
        AccessDataSource1.Updating += AccessDataSource1_Cancel;
        AccessDataSource1.Inserting += AccessDataSource1_Cancel;
        AccessDataSource1.Deleting += AccessDataSource1_Cancel;
    }

    void AccessDataSource1_Cancel(object sender, SqlDataSourceCommandEventArgs e) {
        e.Cancel = true;
    }
}
See Also