我想在Gridview中找到总价格但是当我这样做时,我必须跟踪这件作品。例如,如果我买了4双鞋,而且鞋子的价格是20,我应该20 * 4总价。我怎么能在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;
}
}
答案 0 :(得分:1)
您可以简单地执行此操作以显示页脚中的总数。为了简单起见,我省略了异常处理。
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;
}
}