Back to Devexpress

ASPxGridView.AddNewRow() Method

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

latest6.1 KB
Original Source

ASPxGridView.AddNewRow() Method

Adds a new record.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public void AddNewRow()
vb
Public Sub 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.

aspx
<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>
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;

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;
    }
}
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

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

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not IsPostBack Then
            gridView.AddNewRow()
        End If
        DisableUpdateInDemo()
    End Sub
    Protected Sub ASPxGridView1_BeforeGetCallbackResult(ByVal sender As Object, ByVal e As EventArgs)
        If (Not gridView.IsEditing) AndAlso (Not gridView.IsNewRowEditing) Then
            gridView.AddNewRow()
        End If
    End Sub
    Protected Sub gridView_RowDeleting(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataDeletingEventArgs)

    End Sub
    Protected Sub gridView_RowInserting(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataInsertingEventArgs)

    End Sub

    'Database update is not allowed in online demos
    Private Sub DisableUpdateInDemo()
        AddHandler AccessDataSource1.Updating, AddressOf AccessDataSource1_Cancel
        AddHandler AccessDataSource1.Inserting, AddressOf AccessDataSource1_Cancel
        AddHandler AccessDataSource1.Deleting, AddressOf AccessDataSource1_Cancel
    End Sub

    Private Sub AccessDataSource1_Cancel(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs)
        e.Cancel = True
    End Sub
End Class

See Also

InitNewRow

Grid View

ASPxGridView Class

ASPxGridView Members

DevExpress.Web Namespace