vb.net 在绑定到数据源的 DevExpress AspxGridView 中添加新行/记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26419840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:22:08  来源:igfitidea点击:

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

asp.netvb.netwebformsdevexpressaspxgridview

提问by Faizan Mubasher

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.

我的问题很简单。我想获取新插入的行值,并想相应地更新 GridView 和我的数据源。

New Record

新纪录

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

如图中突出显示的区域所示,我想在代码隐藏文件中获取此电子邮件和其他字段。我正在使用WebForms/VB.NET.

Here is my aspxcode.

这是我的aspx代码。

<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.

此链接令人困惑:插入新行

Notethat I am not using DataTable.

请注意,我没有使用DataTable.

采纳答案by Niranjan Singh

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.

正如我怀疑您没有遗漏 AspxGridView 实体框架的某些实现。您正在将 EnitiyFramework 数据源分配给网格。可能您只是在创建一些 List 数据源并分配它。

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.

我建议您阅读以下知识库和示例。它将让您知道如何在使用与 EntityDataSource / Entity Framework 绑定的 ASPxGridView 时实现常见场景。

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

使用与EntityDataSource/Entity Framework绑定的ASPxGridView时如何实现常见场景
使用匿名类型时如何在与LinqDataSource绑定的ASPxGridView中利用CRUD操作
绑定到EntityFramework的ASPxGridView - 如何更新多个表中的数据

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

根据文档,您将从 e.NewValues 字典中大胆地获取 RowInserting 事件的值。

Manual CRUD Operations:

手动 CRUD 操作:

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.

1) 插入: - 处理 ASPxGridView.RowInserting 事件;
- 创建一个 DataContext 实例;
- 创建一个新的 DataItem 并从 e.NewValues 字典中填充其属性;
- 在对应的表中添加一个新的DataItem;
- 提交更改;
- 将 eventArgs e.Cancel 属性设置为“true”以取消插入操作;
- 调用 ASPxGridView.CancelEdit 方法关闭 EditForm。

回答by AntPe

You can try this:

你可以试试这个:

  1. Close the editing form
  2. Reload data on the aspxgridview (This will refresh both the datasource as the datagrid):
  1. 关闭编辑表单
  2. 重新加载 aspxgridview 上的数据(这将刷新数据源作为数据网格):

I'll put this in c# but the concept is the same:

我会把它放在 c# 中,但概念是一样的:

protected void dxGvCatalog_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
{       
    // Here your code to save

    // Close edit form
    dxGvCatalog.CancelEdit();
    // reload Grid
    reloadAspxGridView();
}

public void reloadAspxGridView()
{
    dxGvCatalog.DataSource = Class.ConsultAll();     // Change this to your data source
    dxGvCatalog.Enabled = true;
    dxGvCatalog.Visible = true;
    dxGvCatalog.DataBind();
}