简体   繁体   中英

AspxGridview Add new row and retain last row value

I have a aspxgridview that will allow the user to add new row. What I want to do is to retain the value of last row everytime the new row has been added. I have the Add new row button outside the gridview. Is it possible? Any help would be much appreciated.

Below is what I have so far.

Front-end

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

Back-end

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

Solution: You should Create the empty(Add New) row from client side instead of creating from server side. change the button code in front end like following,

    <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 -- here explained how we can call the server side events dynamically from client side. instead of use the grid command button column option or default button.

And as per our chat discussion,

It seems you are trying batch update. You can do batch update by two ways. 1. Individually (CRUD) 2. at once (Batch update).

1. Individually (CRUD - ASPxGridView RowInserting/RowUpdating/RowDeleting events) Use If you want to submit a batch of modifications individually Ie ASPxGridView RowInserting/RowUpdating/RowDeleting events

2. At once (Batch update - ASPxGridView.BatchUpdate event): Use If you want to submit a batch of modifications at once ( ASPxGridView.BatchUpdate event)

Both the above procedures are clearly explained here

I hope it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM