ASPxGridView和LinqServerModeDataSource,按存储过程插入行 [英] ASPxGridView and LinqServerModeDataSource, inserting rows by stored procedure

查看:67
本文介绍了ASPxGridView和LinqServerModeDataSource,按存储过程插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用ASPxGridView的页面,并填充GridView,我使用LinqServerModeDataSource控件,该控件通过MS SQL Server的SQL视图获取数据.到目前为止还不错,但是问题来了.

I have a page using ASPxGridView and to fill the GridView I use a LinqServerModeDataSource control who fetches it data through a SQL View from a MS SQL Server. So far so good but here comes the problem.

我想在数据库中插入数据,所以我要构建一个自定义表单

I want to insert data in the database so I'm building a custom form

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

以下是自定义表单的示例

Here is an example of the custom form

相当基本的东西,在这种情况下,公司名称进入一个表,电子邮件发送至另一个表.所有这些都通过调用存储过程来处理.或多数民众赞成在计划.

Pretty basic stuff, in this scenario the Company name goes to one table and the email to another. This is all handled by calling a Stored Procedure. Or thats the plan.

有人知道该怎么做吗?

感觉就像我一直在尝试所有操作,LinqServerModeDataSource1_Inserting,ASPxGridView1_RowInserting等,但是没有.这是失败的代码之一.而且devexpress文档甚至更糟,但不要在那儿放一个不愉快的地方.

It feels like I've been trying it all, LinqServerModeDataSource1_Inserting, ASPxGridView1_RowInserting and so on but nooo. Here is one of the failing codes. And the devexpress documentation is even worse, but lets not go there its not a happy place.

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

因此,我们非常感谢您提供任何建议或帮助.

So, any advice or help is highly appreciated.

谢谢!

推荐答案

我想在数据库中插入数据,以便建立自定义表单" ASPxGridView可以为您完成所有这些操作,而无需创建自定义模板至少与sqldb如果我要在sqldatasource下方定义删除命令和/或编辑命令,我已经可以在gridview中删除和编辑信息

"I want to insert data in the database so I'm building a custom form" ASPxGridView can do all that for you without you having to make a custom template at least with sqldb If i would define a delete command and or edit command below to the sqldatasource i could already delete and edit the information in the 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>

但是,如果您确实要使用自定义的插入或删除功能,则可以

But you can if you really want to use a custom made insert or delete

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

这就是我上次这样做的方式.我利用了数据表并将其存储在会话中,只是尝试一下,但它可以正常工作.

That's how i did it last time. I made use of datatables and stored that in sessions bla bla realy just trying out but it works alright.

这篇关于ASPxGridView和LinqServerModeDataSource,按存储过程插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆