Reputation: 263
I want to find the total price in Gridview but when I do this I have to keep track of the piece. For example If I bought 4 shoes, and And the price of 1 shoes is 20. I should 20*4 for total price. How can I do that in GridView
decimal price = 0;
int piece = 0;
decimal totalPrice = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
price += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "price"));
piece += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "piece"));
totalPrice = price * piece;
}
else if(e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total Price:";
e.Row.Cells[2].Text = totalPrice.ToString("c");
e.Row.Cells[1].HorizontalAlign = e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
e.Row.Font.Bold = true;
}
}
Upvotes: 0
Views: 445
Reputation: 499
You can simply do this to show the total in footer. I am omitting exception handling for simplicity here.
decimal price = 0;
int piece = 0;
decimal totalPrice = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// This will multiply the price and piece and add it to final total.
totalPrice += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "price")) * Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "piece"));
}
else if(e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total Price:";
e.Row.Cells[2].Text = totalPrice.ToString("c");
e.Row.Cells[1].HorizontalAlign = e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
e.Row.Font.Bold = true;
}
}
Upvotes: 1