2.22. Adding a Totals Row to a GridView
You have a
GridView containing numeric information, and you need to display a total of the data in the last row of the grid.
Enable the output of the footer in the
GridView, accumulate the total for the data in the
RowDataBound event handler, and then output the total in the
In the .aspx file, set the
ShowFooter attribute of the
asp:GridView element to
In the code-behind class for the page, use the .NET language of your choice to:
Initialize the totals to
0, and bind the data to the
GridViewin the normal fashion.
RowDataBoundevent handler, add the values for each data row to the accumulated totals.
RowDataBoundevent handler, set the total values in the footer when the footer is data bound.
Figure 2-25. GridView with totals row output
The best way to describe the addition of a totals row to a
GridView is by example. In this recipe, you’ll want to create the
GridView differently than normal. In the
asp:GridView element, set the
ShowFooter attribute to
True to cause a footer to be output when the control is rendered. Then, you place the totals data in the footer.
<asp:GridView ID="gvBooks" Runat="Server" AllowPaging="false" AllowSorting="false" ...