Friday, July 8, 2011

How to show sum at footer in Gridview.

Generally to show sum at footer in GridView we use. GridView's RowDataBound Event. We Create a variable(float/Int) outside of event.
and in RowDataBound Event we check row type. if row type is DataRow than we find the Cell's Text convert it into int and Add it to variable created outside of RowDataBound Event. and When RowType change from DataRow to Footer Row than we put that variable's value in footer.
But i want to tell you short and sweet method for it.
as all of you know generally we use DataTable as  datasource in gridview.
than we should use it
string str = dt.Compute("sum(sGrandTotal)", "").ToString();
now just find the Footer of gridview and put str variable in the cell. like this
grdReport.FooterRow.Cells[7].Text = "Total" + float.Parse(str).ToString( "N2");

No comments:

Post a Comment