Search code examples
asp.netvb.netwebformsdevexpressaspxgridview

Adding new row/record in DevExpress AspxGridView which is Bind to DataSource


My question is very simple. I want to get the newly inserted values of row and want to update GridView and my Data Source accordingly.

New Record

As shown in highlighted area in image, I want to get this Email and other fields in the code behind file. I am using WebForms/VB.NET.

Here is my aspx code.

<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" EnableTheming="True" Theme="DevEx" 
        OnDataBinding="ASPxGridView1_DataBinding" 
        OnRowUpdating="ASPxGridView1_RowUpdating"
        OnRowInserting="ASPxGridView1_RowInserting"
        OnRowInserted="ASPxGridView1_RowInserted"
        >
        <Columns>
            <dx:GridViewCommandColumn VisibleIndex="0">
                <EditButton Visible="True"/>
                <NewButton Visible="True"/>
                <DeleteButton Visible="True" />
            </dx:GridViewCommandColumn>
            <dx:GridViewDataColumn FieldName="Email" VisibleIndex="2" Name="Email"> 
                <EditFormSettings Caption="Email" />
            </dx:GridViewDataColumn>
            <dx:GridViewDataColumn FieldName="FirstName" VisibleIndex="3" Name="FirstName" />
            <dx:GridViewDataColumn FieldName="LastName" VisibleIndex="4" Name="LastName" />
            <dx:GridViewDataColumn FieldName="Password" VisibleIndex="5" Name="Password" />
            <dx:GridViewDataColumn FieldName="RetryCount" VisibleIndex="6" Name="RetryCount" />
            <dx:GridViewDataColumn FieldName="MaxRetryCount" VisibleIndex="7" Name="MaxRetryCount" />
        </Columns>
        <SettingsPopup>
            <EditForm Width="600" />
        </SettingsPopup>
    </dx:ASPxGridView>

And this is my code behind.

Protected Sub ASPxGridView1_RowInserting(sender As Object, e As DevExpress.Web.Data.ASPxDataInsertingEventArgs)
    Dim gridView As ASPxGridView = CType(sender, ASPxGridView)

    ' What to write here???
    e.NewValues("Email") 'doesn't give anything
    e.NewValues("Email") = "SomeEmail" 'It is also not working

End Sub

This link is confusing: Inserting new Row.

Note that I am not using DataTable.


Solution

  • As i suspect you are not missing something implementation with Entity Framework of AspxGridView. You are assigning EnitiyFramework datasource to the grid. May be you are just creating some List data source and assigning it.

    I suggest you to go through the below KB and Examples. It will let you know that how to implement common scenarios when using ASPxGridView bound with EntityDataSource / Entity Framework.

    References:

    How to implement common scenarios when using ASPxGridView bound with EntityDataSource / Entity Framework
    How to utilize CRUD operations within ASPxGridView bound with LinqDataSource when using Anonymous Types
    ASPxGridView bound to EntityFramework - How t update data in several tables

    According to documentation, You will defiantly get values on the RowInserting event from e.NewValues dictionary.

    Manual CRUD Operations:

    1) Inserting: - Handle the ASPxGridView.RowInserting event;
    - Create a DataContext instance;
    - Create a new DataItem and fill its properties from the e.NewValues dictionary;
    - Add a new DataItem to the corresponding Table;
    - Submit Changes;
    - Set the eventArgs e.Cancel property to "true" to cancel an inserting operation;
    - Call the ASPxGridView.CancelEdit method to close the EditForm.