【发布时间】:2016-05-11 22:06:38
【问题描述】:
我正在尝试使用 for 循环获取网格视图列的总和并将其放入标签中
我尝试了下面的代码,但结果不正确
decimal total = 0;
protected void gridpandl_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int index = 0; index < this.gridpandl.Rows.Count; index++)
{
string proid = gridpandl.DataKeys[index].Value.ToString();
string Purchase = GetPurchase(proid);
this.gridpandl.Rows[index].Cells[2].Text = Purchase;
total += Convert.ToDecimal(Purchase);
lbltotal.Text = Convert.ToString(total);
}
}
【问题讨论】:
-
也许我错了,但这个事件被称为 foreach 行。因此,如果您有两行价值为 10 的购买,您最终会得到总共 40。我认为您应该在 DataBind() 调用之后移动循环,而不是在 RowDataBoud 事件中
-
谢谢你是对的