Skip to content
Advertisement

How can I modify the value of an element of my gridview before update in database?

I have a gridview but I want to update a cell value before the update in the database but it doesn’t work

In the .aspx file:

<asp:GridView ID="GV_CT" runat="server" AutoGenerateColumns="False" 
    DataSourceID="DS_CT" EnableViewState="False" Width="100%" 
    DataKeyNames="IdCT" ShowHeader="true" OnRowUpdating="GV_CT_RowUpdating">
    <Columns>
        <asp:BoundField DataField="qty" HeaderText="Quantity"/>
        <asp:BoundField DataField="price" HeaderText="Price" DataFormatString="{0:c}"/>
        <asp:CommandField ShowEditButton="true" ItemStyle-Width="75" ControlStyle-Width="30" ItemStyle-HorizontalAlign="Center" ShowCancelButton="true" EditText='<i class="fas fa-pencil"></i>' UpdateText='<i class="fas fa-check"></i>' CancelText='<i class="fas fa-times"></i>'/>
    </Columns>
</asp:GridView>

<asp:SqlDataSource ID="DS_CT" runat="server" ConnectionString="connectionstring" SelectCommand="SelectCommand" SelectCommandType="StoredProcedure" UpdateCommand="UpdateCommand" UpdateCommandType="StoredProcedure" >
    <SelectParameters>
        <asp:Parameter Direction="ReturnValue" Name="RETURN_VALUE" Type="Int32" />
        <asp:QueryStringParameter Name="idcustomer" QueryStringField="idcustomer" Type="String" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Direction="ReturnValue" Name="RETURN_VALUE" Type="Int32" />
        <asp:Parameter Name="IdCT" Type="Int32" />
        <asp:Parameter Name="qty" Type="Int32" />
        <asp:Parameter Name="price" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>

In the .aspx.cs file:

protected void GV_CT_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
        if (e.OldValues["price"] != null)
        {
            // I try this
            DS_Ct.UpdateParameters.Add("price", "50");

            // I also try this
            e.OldValues["price"] = "50" ;
        }
}

I try to update the row before insert in database with OnRowUpdating but it doesn’t change the value. Someone have any idea how to do it ?

Advertisement

Answer

I have to do it in the sql finally.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement