gridview的摘要

时间:2013-03-21 06:25:38

标签: c# asp.net

我需要在页脚上进行gridview摘要...

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {
        decimal totalPrice = 0M;
        int totalItems = 0;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblPrice = (Label)e.Row.FindControl("lblPrice");
            decimal price = Decimal.Parse(lblPrice.Text); 
            totalPrice += price;
            totalItems += 1; 
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lblTotalPrice = (Label)e.Row.FindControl("lblTotalPrice");
            lblTotalPrice.Text = totalPrice.ToString(); 
        }
 }

但它不起作用。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

将totalPrice和totalItems声明为globel变量,如下所示

decimal totalPrice = 0M;
int totalItems = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    ...

   }

答案 1 :(得分:0)

在您的方法中,totalPrice在方法范围内声明,因此它将从每行的零开始。当涉及页脚行时,它将再次为零。至少有两个选择:

  1. 在您的SQL语句中包含total = sum(price)列并绑定 它是你的页脚;
  2. 从您的方法将totalPrice移至页面的私有字段。在 这种情况下你的代码应该正常工作