我有一个aspxgridview,它将允许用户添加新行。我想做的是每次添加新行时都保留最后一行的值。我在gridview外面有Add new row按钮。可能吗?任何帮助将非常感激。

以下是到目前为止的内容。

前端

<dx:ASPxGridView ID="ASPxGridView1" ProcessSelectionChangedOnServer="true"
                    ClientInstanceName="sampleGrid" runat="server" AutoGenerateColumns="False"
                    DataSourceID="forFromGrid" Theme="Material" Font-Size="11px" KeyFieldName="thisid"
                    Width="100%" CssClass="body"
                        OnHtmlFooterCellPrepared="ASPxGridView1_HtmlFooterCellPrepared" >
                    <SettingsBehavior ColumnResizeMode="NextColumn" />
                    <Settings VerticalScrollBarMode="Visible" VerticalScrollableHeight="345"  />
                    <settingscommandbutton>
                        <showadaptivedetailbutton buttontype="Image">
                        </showadaptivedetailbutton>
                        <hideadaptivedetailbutton buttontype="Image">
                        </hideadaptivedetailbutton>
                    </settingscommandbutton>
                    <SettingsDataSecurity AllowEdit="False" AllowInsert="False" />

                    <Columns>

                    <dx:GridViewDataTextColumn FieldName="thisid" ShowInCustomizationForm="True"
                            VisibleIndex="0" ReadOnly="True" Visible="false">
                        <SettingsHeaderFilter>
                        <DateRangePickerSettings EditFormatString="" />
                        </SettingsHeaderFilter>
                        <EditFormSettings Visible="False" />
                    </dx:GridViewDataTextColumn>

                    <dx:GridViewDataTextColumn FieldName="no." Visible="false"
                        ShowInCustomizationForm="True" VisibleIndex="1">

                    </dx:GridViewDataTextColumn>

                    <dx:GridViewDataTextColumn FieldName="product"  visible="false"
                        ShowInCustomizationForm="True" VisibleIndex="2">
                    </dx:GridViewDataTextColumn>

                    <dx:GridViewDataComboBoxColumn FieldName="product id" Caption="Product"
                        ShowInCustomizationForm="True" VisibleIndex="3">
                     <DataItemTemplate>
                            <dx:ASPxComboBox ID="cmdProduct" runat="server" DataSourceID="Product" ValueType="System.String"
                            ValueField="p_pcode" TextField="p_pname" OnInit="cmbProduct_Init" Width="100%" Theme="Material"
                            IncrementalFilteringDelay="600" CallbackPageSize="20" EnableCallbackMode="true" DropDownStyle="DropDown">
                                <Border BorderStyle="None" />
                            </dx:ASPxComboBox>
                            <asp:SqlDataSource ID="Product" runat="server"
                            ConnectionString="<%$ ConnectionStrings:BMS %>"
                            SelectCommand="SELECT [code], [name] FROM [product] WHERE ([status] = @stat) order by [name] asc ">
                            <SelectParameters>
                            <asp:Parameter DefaultValue="A" Name="stat" Type="String" />
                            </SelectParameters>
                            </asp:SqlDataSource>
                        </DataItemTemplate>
                    </dx:GridViewDataComboBoxColumn>

                    <dx:GridViewDataTextColumn FieldName="variation" visible="false"
                        ShowInCustomizationForm="True" VisibleIndex="4">

                    </dx:GridViewDataTextColumn>

                    <dx:GridViewDataComboBoxColumn FieldName="varid" ShowInCustomizationForm="True" Caption="Version"
                        VisibleIndex="5">
                       <DataItemTemplate>
                        <dx:ASPxComboBox ID = "cmbVariant" runat="server" DataSourceID="Variation" ValueType="System.String"
                        ValueField="v_matid" TextField="v_var" OnInit="cmbVar_Init" Width="100%" Theme="Material"
                        IncrementalFilteringMode="Contains" IncrementalFilteringDelay="600" CallbackPageSize="20"
                        EnableCallbackMode="true" DropDownStyle="DropDown">
                            <Border BorderStyle="None" />
                        </dx:ASPxComboBox>
                        <asp:SqlDataSource ID="Var" runat="server"
                        ConnectionString="<%$ ConnectionStrings:BMS %>"
                        SelectCommand="SELECT [varid], [varcode], [var] FROM [variant] WHERE (([varcode] = @varcode) AND ([varstat] = @varstat))">
                        <SelectParameters>
                            <asp:SessionParameter Name="varcode" DefaultValue="" SessionField="varid" Type="String" />
                            <asp:Parameter DefaultValue="A" Name="varstat" Type="String" />
                        </SelectParameters>
                        </asp:SqlDataSource>
                     </DataItemTemplate>
                    </dx:GridViewDataComboBoxColumn>


                    <dx:GridViewDataTextColumn FieldName="place" visible="false"
                        ShowInCustomizationForm="True" VisibleIndex="8">

                    </dx:GridViewDataTextColumn>

                    <dx:GridViewDataComboBoxColumn FieldName="placeid" Caption="Program"
                        ShowInCustomizationForm="True" VisibleIndex="9">
                     <DataItemTemplate>
                             <dx:ASPxComboBox ID="cmbPlace" runat="server" ValueType="System.String" DataSourceID="Place" Theme="Material"
                             ValueField = "pg_place" TextField="pg_plcname" Width="100%" CssClass="EditDropdown" OnInit="cmbPlace_Init"
                             IncrementalFilteringMode="Contains" IncrementalFilteringDelay="600" CallbackPageSize="20"
                             EnableCallbackMode="true" DropDownStyle="DropDown">
                                <Border BorderStyle="None" />
                             </dx:ASPxComboBox>
                             <asp:SqlDataSource ID="Place" runat="server"
                             ConnectionString="<%$ ConnectionStrings:BMS %>"
                             SelectCommand="SELECT [pg_plccode], [pg_plcname] FROM [place] WHERE ([stat] = @stat) order by pg_plcname asc">
                             <SelectParameters>
                             <asp:Parameter DefaultValue="A" Name="pg_stat" Type="String" />
                             </SelectParameters>
                             </asp:SqlDataSource>
                         </DataItemTemplate>
                    </dx:GridViewDataComboBoxColumn>





                    <dx:GridViewDataTextColumn FieldName="cost" Caption="Internal Cost"
                        ShowInCustomizationForm="True" VisibleIndex="12">
                       <DataItemTemplate>
                                        <dx:ASPxSpinEdit ID="acost" DecimalPlaces="2" DisplayFormatString="N" OnInit="IntCost_Init" Width="100%" Theme="Material"
                                        CssClass="EditDropdown" runat="server" Number=" 0" SpinButtons-ClientVisible="false" AllowMouseWheel="false" HorizontalAlign="Right">
                                            <Border BorderStyle="None" />
                                        </dx:ASPxSpinEdit>
                                        </DataItemTemplate>
                    </dx:GridViewDataTextColumn>

                    <dx:GridViewDataTextColumn FieldName="chnfr_rkey" visible="false"
                        ShowInCustomizationForm="True" VisibleIndex="13">

                    </dx:GridViewDataTextColumn>

                    </Columns>
                      <StylesPager Button-HorizontalAlign="Right" Button-HoverStyle-BackgroundImage-HorizontalPosition="right"></StylesPager>
                    <Templates>
                        <PagerBar>
                        <table width="100%" class="tablebutton_change">
                            <tr style="float:right;">

                            <td class = "btn_setting">
                            <dx:ASPxButton ID="btn_addnew"  runat="server"  Text="Add new row" Theme="Mulberry" CssClass="btn_change"
                            AutoPostBack="true" Image-Url="~/images/icons/addnewrow.png" CausesValidation="false"
                            UseSubmitBehavior="false" OnClick="AddnewClick">
                            </dx:ASPxButton>
                            </td>
                            </tr>
                            </table>

                        </PagerBar>
                    </Templates>
                    <settingspager AlwaysShowPager="true">
                    </settingspager>

                    <SettingsBehavior ProcessSelectionChangedOnServer="True" />
                    <Styles Header-HorizontalAlign="Center">
                        <header horizontalalign="Center">
                        </header>
                    </Styles>


                    </dx:ASPxGridView>


后端

Protected Sub AddnewClick(sender As Object, e As System.EventArgs)
        Dim cn As New SqlConnection(ConfigurationManager.ConnectionStrings("sales").ConnectionString)
        Dim cmd As New SqlCommand
        cmd.CommandText = "insert"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = cn
        cn.Open()
        cmd.Parameters.AddWithValue("@idno", Session("idno").ToString)
        cmd.Parameters.AddWithValue("@product", DBNull.Value)
        cmd.Parameters.AddWithValue("@productid", DBNull.Value)
        cmd.Parameters.AddWithValue("@variant", DBNull.Value)
        cmd.Parameters.AddWithValue("@varid", DBNull.Value)
        cmd.Parameters.AddWithValue("@palce", DBNull.Value)
        cmd.Parameters.AddWithValue("@palceid", DBNull.Value)
        cmd.Parameters.AddWithValue("@cost", DBNull.Value)
        cmd.Parameters.AddWithValue("@date", Date.Today)
        cmd.ExecuteNonQuery()
        cn.Close()
        cmd.Parameters.Clear()

        forFromGrid.SelectParameters(0).DefaultValue = Session("idno").ToString
        fordtl.SelectParameters(0).DefaultValue = Session("idno").ToString
        ASPxGridView1.DataBind()
    End Sub

最佳答案

解:
您应该从客户端而不是服务器端创建空(添加新行)。如下更改前端的按钮代码,

    <dx:ASPxButton ID="btn_addnew" runat="server" Text="Add new row"
    Theme="Mulberry" CssClass="btn_change" AutoPostBack="false"
Image-Url="~/images/icons/addnewrow.png" CausesValidation="false" UseSubmitBehavior="false">
        <ClientSideEvents Click="function(s, e) {
        sampleGrid.AddNewRow();
        }" />
        </dx:ASPxButton>


https://documentation.devexpress.com/AspNet/DevExpress.Web.Scripts.ASPxClientGridView.AddNewRow.method -在此说明了如何从客户端动态调用服务器端事件。而不是使用网格命令按钮列选项或默认按钮。

根据我们的聊天讨论,

看来您正在尝试批量更新。您可以通过两种方式进行批量更新。 1.单独(CRUD)2.一次(批量更新)。

1.单独(CRUD-ASPxGridView RowInserting / RowUpdating / RowDeleting事件)使用如果要单独提交一批修改,即ASPxGridView RowInserting / RowUpdating / RowDeleting事件

2.一次(批处理更新-ASPxGridView.BatchUpdate事件):使用如果要一次提交一批修改(ASPxGridView.BatchUpdate事件)

以上两个步骤均已明确说明here

希望对您有帮助。

08-07 09:58