locked
Gridview total price RRS feed

  • Question

  • User-2094959909 posted

    Hello everyone, 

    i work on an application, so i can add some products with name, price, quantity, ... , so now i show in a gridview all the products added and when i want to finish of adding the products i need to show under the gridview the total of all the prices of the products 

    if anybody can help please.

    Thanks in advance.

    Wednesday, October 24, 2018 4:57 PM

Answers

  • User61956409 posted

    Hi Omar27,

    eed to show under the gridview the total of all the prices of the products

    To achieve the requirement, you can refer to the following sample code.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="true" OnRowDataBound="GridView1_RowDataBound" FooterStyle-CssClass="">
        <Columns>
            <asp:BoundField DataField="pname" HeaderText="Name" />
            <asp:BoundField DataField="pprice" HeaderText="Price" />
            <asp:BoundField DataField="pquantity" HeaderText="Quantity" />
        </Columns>
    </asp:GridView>
    int total = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
                
        if (!IsPostBack)
        {
            bindgrid();
        }
    }
    
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Increment the running totals
    
            total += Convert.ToInt32(e.Row.Cells[1].Text) * Convert.ToInt32(e.Row.Cells[2].Text);
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            //Display the summary data in the footer
            e.Row.Cells[1].Text = "Total";
            e.Row.Cells[2].Text = total.ToString();
        }
    }

    Test Result:

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 25, 2018 2:41 AM