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

问题描述:

我的问题很简单。我想获取新插入的行值,并且想要相应地更新GridView和我的数据源。在DevExpress AspxGridView中添加绑定到数据源的新行/记录

New Record

由于在图像高光区域显示,我要得到这个电子邮件等领域,在代码隐藏文件。我正在使用WebForms/VB.NET

这是我的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> 

这是我的代码背后。

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 

此链接令人困惑:Inserting new Row

注意,我不使用DataTable

+0

那么你怎么提供数据源到电网。您应该能够在RowInserting事件中获取值。 – 2014-10-17 10:22:44

+0

使用实体框架。 – 2014-10-17 10:34:55

因为我怀疑你不会错过实体的实体 AspxGridView的框架。您正在将EnitiyFramework 数据源分配给网格。可能你只是创建了一些List 数据源并分配它。

我建议你通过下面的KB和范例。它会让你知道如何在使用与EntityDataSource/Entity Framework绑定的ASPxGridView时实现常见场景。

参考文献:

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

根据文件,你会得到挑衅从e.NewValues字典RowInserting事件值。

手册CRUD操作:

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

你可以试试这个:

  1. 关闭编辑表单
  2. 刷新数据的aspxgridview(这将刷新两个 数据源的数据网格):

我把这在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(); 
}