我想在gridview中总结两列,并在标签中显示总数。我在gridview中启用了分页。
使用此代码我将两列的总和相加,但总数仅适用于所选页面。
如何执行总计来汇总所有页面中所选列的所有行?
decimal priceTotal = 0;
int totalMinutes = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
priceTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Total"));
totalMinutes += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Duration"));
}
lblTotalPrice.Text = "Total Cost: $"+ priceTotal.ToString();
lblTotalMinutes.Text = "Total duration: " + totalMinutes.ToString();
}
答案 0 :(得分:1)
decimal price = 0;
foreach (DataGridViewRow row in DataGridView.Rows)
{
price += row.Cells["Total"]!=null ? Convert.ToDecimal(row.Cells["Total"].Value) : 0;
}
lblTotalPrice.Text = "Total Cost: $"+ price.ToString("n2");