gridview中的标题文本更改为零

时间:2015-12-17 15:38:32

标签: c# asp.net gridview

我使用以下代码在网格视图中获取DataField="Quantity"的总和,并在网格视图页脚中显示结果

问题是HeaderText="Quantity"更改为零

protected void griddelverynote_RowDataBound(object sender, GridViewRowEventArgs e)
{
     int TotalQuantity = 0;
     if (e.Row.RowType == DataControlRowType.DataRow)
     TotalQuantity += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Quantity"));
     else if (e.Row.RowType == DataControlRowType.Footer)
     e.Row.Cells[1].Text = "Total Quantity";
     e.Row.Cells[2].Text = TotalQuantity.ToString();
}

1 个答案:

答案 0 :(得分:0)

代码中的Probelm是绑定时为每一行触发的RowDataBound事件。因此,每行TotalQuantity都会重新初始化为0。在外面声明变量TotalQuantity: -

int TotalQuantity = 0;
protected void griddelverynote_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
          TotalQuantity += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Quantity"));
     else if (e.Row.RowType == DataControlRowType.Footer)
     {
          e.Row.Cells[1].Text = "Total Quantity";
          e.Row.Cells[2].Text = TotalQuantity.ToString();
     }
}