2011-06-07 67 views
0

我有一个页面使用ASPxGridView并填充GridView我使用LinqServerModeDataSource控件通过SQL视图从MS SQL Server中获取数据。到目前为止这么好,但问题来了。ASPxGridView和LinqServerModeDataSource,通过存储过程插入行

我想在数据库中插入数据,所以我建立一个自定义窗体

<Templates> 
    <EditForm> 
     Company Name: <dx:ASPxTextBox ID="CompanyName" runat="server" /> 
     Company Mail: <dx:ASPxTextBox ID="Email" runat="server" /> 

     <dx:ASPxGridViewTemplateReplacement ID="UpdateButton" ReplacementType="EditFormUpdateButton" runat="server" /> 
     <dx:ASPxGridViewTemplateReplacement ID="CancelButton" ReplacementType="EditFormCancelButton" runat="server" /> 

    </EditForm> 
</Templates> 

这里是自定义窗体的例子

Example

相当基本的东西,在这公司名称将转至一个表,并将电子邮件发送至另一个表。这全部通过调用存储过程来处理。或者那是计划。

有谁知道如何做到这一点?

感觉就像我一直在尝试这一切,LinqServerModeDataSource1_Inserting,ASPxGridView1_RowInserting等,但没有。这是失败的代码之一。而devexpress文档更糟糕,但不要去那里不愉快的地方。

protected void LinqServerModeDataSource1_Inserting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceEditEventArgs e) 
{ 
    // Some magic to insert data here 
    ASPxGridView1.CancelEdit(); 
} 

因此,任何意见或帮助,高度赞赏。

谢谢!

回答

1

“我想在数据库中插入数据,所以我正在构建一个自定义窗体”ASPxGridView可以为您做所有这些工作,而无需至少使用sqldb创建自定义模板 如果我要定义一个删除命令并以下为SqlDataSource的编辑命令我可能已经删除,并在GridView

<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" 
     ClientIDMode="AutoID" DataSourceID="SqlDataSource1"> 
     <Columns> 
      <dx:GridViewCommandColumn VisibleIndex="0"> 
       <EditButton Visible="True"> 
       </EditButton> 
       <NewButton Visible="True"> 
       </NewButton> 
       <DeleteButton Visible="True"> 
       </DeleteButton> 
      </dx:GridViewCommandColumn> 
      <dx:GridViewDataTextColumn FieldName="Naam" VisibleIndex="0"> 
      </dx:GridViewDataTextColumn> 
      <dx:GridViewDataTextColumn FieldName="Adres" VisibleIndex="1"> 
      </dx:GridViewDataTextColumn> 
      <dx:GridViewDataTextColumn FieldName="Postcode" VisibleIndex="2"> 
      </dx:GridViewDataTextColumn> 
      <dx:GridViewDataTextColumn FieldName="Plaats" VisibleIndex="3"> 
      </dx:GridViewDataTextColumn> 
     </Columns> 
    </dx:ASPxGridView> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
     SelectCommand="SELECT [Name], [Address], [Postcode], [City] FROM [Somewhere]"> 
    </asp:SqlDataSource> 

编辑的信息,但你可以,如果你真的想用一个定制的插入或删除

protected void ASPxGridView4_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e) 
{ 
    DataColumn[] esl = new DataColumn[] { dt.Columns["SQLWaarde"] }; 
    if (ds.Tables[0].PrimaryKey == null || ds.Tables[0].PrimaryKey == esl) 
    { 
     ds.Tables[0].PrimaryKey = new DataColumn[] { dt.Columns["SQLWaarde"] }; 
    } 
    DataRow[] delRow = ds2.Tables[0].Select("IndWaardeType = '" + e.Values[1] + "' AND SQLWaarde = '" + e.Values[2] + "'"); 
    ds2.Tables[0].Rows.Remove(delRow[0] as DataRow); 

    e.Cancel = true; 
    ASPxGridView4.CancelEdit(); 
    Session["ds2"] = ds2; 
} 
protected void ASPxGridView4_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) 
{ 
    ndr2 = ds2.Tables[0].NewRow(); 
    ndr2[0] = e.NewValues[0]; 
    ndr2[1] = e.NewValues[1]; 
    ndr2[2] = e.NewValues[2]; 
    ds2.Tables[0].Rows.Add(ndr2); 

    e.Cancel = true; 
    ASPxGridView4.CancelEdit(); 
    Session["ds2"] = ds2; 
} 

那怎么我上次做了。我使用了数据表,并将它存储在会话中,但最后只是尝试,但它工作正常。