答案 0 :(得分:0)
您可以使用RowDataBound事件。
//create a variable outside the rowdatabound method
int total_sum = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//cast the row back to a datarowview
DataRowView row = e.Row.DataItem as DataRowView;
//or if a list<object> is bound to the gridview
//MyClass row = e.Row.DataItem as MyClass;
//cast the correct column to an int and add it to the total
total_sum += Convert.ToInt32(row["itemid"]);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
//show results. this wil work even if the footer is not shown
Label1.Text = string.Format("{0:N0}", total_sum);
}
}