Tuesday, May 12, 2009

get summery total in datagrid C# - rowdatabound event

protected void gvWeeklySummeryReport_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
switch ((int)(e.Row.RowType))
{
case (int)ListItemType.Item:
case (int)ListItemType.AlternatingItem:
//Calculate total for the field of each row and alternating row.
myTotal += Convert.ToDouble(gvWeeklySummeryReport.DataKeys[e.Row.RowIndex].Values["SpentHours"]);
//Format the data, and then align the text of each cell to the right.
e.Row.Cells[3].Attributes.Add("align", "right");
break;
}
}
else if(e.Row.RowType == DataControlRowType.Footer)
{
gvWeeklySummeryReport.FooterStyle.Font.Bold = true;
//Use the footer to display the summary row.
e.Row.Cells[3].Text = "Total hours";
e.Row.Cells[4].Attributes.Add("align", "left");
e.Row.Cells[4].Attributes.Add("align", "center");
e.Row.Cells[4].Attributes.Add("style", "color:black;font-weight:bold");
e.Row.Cells[3].Attributes.Add("style", "color:black;font-weight:bold");
e.Row.Cells[4].Text = myTotal.ToString();
}
}

No comments: