When the GridView is bound to a data source control, the GridView is composed by enumerating the DataSource and building one row at a time. As each GridView row is databound, the GridView's RowDataBound event is fired. This provides page developers an opportunity to tap into the GridView creation process at row-level granularity.
int quantityTotal = 0;
void detailsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
quantityTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"Quantity"));
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "Totals:";
e.Row.Cells[1].Text = quantityTotal.ToString("d");
e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
e.Row.Font.Bold = true;
}
}