Hire my services

If you would like to hire my services, you can now do so by visiting the following link:

Website Design Darlington

Article Statistics

ArticlesLatest Entry
2121-May-2008

Code Bank Statistics

CategoryTipsLatest Entry
Applications420-Feb-2008
Cache118-Apr-2007
Controls926-Jul-2007
CSS107-Sep-2007
Database428-Aug-2007
Dates128-Aug-2007
Email105-Mar-2007
Files123-Apr-2007
HTML101-Mar-2007
Images226-Mar-2007
Pages115-Oct-2007
Sessions122-Nov-2007
SQL Server1518-Mar-2008
Strings309-May-2007
Skip Navigation LinksHome > Articles > Add a total row to a GridView

Add a total row to a GridView

Summary: An example of how to create a re-usable function to add a total row to a GridViewSocialize it

One common piece of functionality that I've often found myself doing is adding totals to a GridView. Generally, the concept involves using the RowDataBound event, keeping a total of each column and then adding this total to a new label in each column. The problem with this method is that it isn't re-usable since the next GridView you create probably won't have the same columns in it.

To combat this, I've created a function that accepts a GridView as a parameter, loops through it and creates the total row for you. It then returns the GridView so that you can use it on your page.

Let's start by creating a simple page which contains a GridView:

Now, let's add the code behind for this page that will populate the GridView with some sample data:


If you run this page, you'll see a simple GridView that has some sample data in it. Some of the columns are numeric, some aren't. There is also no total row.

Now, here's the function I use to loop through the GridView and create the total row. It accepts the GridView as the first parameter, a boolean value to state whether you want the first column to be totalled (the word "Totals:" will be added if not) and an optional CSS class for the total row. The resulting GridView with the total row will then be returned back to the page.

Add this function to your code-behind page and after you've called the DataBind method of the GridView (in the Page Load event), call this function like so:

You'll now see a total row with a total for each column that has numeric values in it.

Possible Enhancements

* Add an optional parameter for the text to be displayed if the column is not numeric (e.g. "N/A")
* Inherit the GridView control and add this functionality to it

Reader Comments

1. Oddmar 13/04/2007 00:25:27

2. Mark Smith 13/04/2007 01:23:53

3. Rick 15/04/2007 16:15:46

4. Mark Smith 16/04/2007 00:21:57

5. Dana 24/04/2007 11:34:00

6. Mark Smith 25/04/2007 07:20:51

7. Daniel Perez 17/05/2007 12:43:44

8. Mark Smith 19/05/2007 06:50:44

9. tushar 03/08/2007 09:00:31

10. Supraj Venkat 14/08/2007 06:41:15

Add your comments

Please keep your comments relevant to this page. Any inappropriate or purely promotional comments may be removed. Email addresses are never displayed but are required so you can confirm your comments.

Your Name:
 
Your Email:
 
Add your comments: