ASPxGridView and LinqServerModeDataSource, inserti

2019-09-01 23:48发布

问题:

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.

Does anyone know how to do this?

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.

Thanks!

回答1:

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