Click here to Skip to main content
13,046,087 members (59,814 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 4 Sep 2006

How to display a totals line in a GridView footer

, 4 Sep 2006
Rate this:
Please Sign up or sign in to vote.
How to display a totals line in a GridView footer.

Sample Image - Totals_line_in_GridView.jpg


This article explains how to display a totals line in a GridView footer. First, bind an XML file to the GridView. Here is the code for this task:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     If Page.IsPostBack = False Then
         Dim oDs As New DataSet
         oDs.ReadXml(Request.PhysicalApplicationPath + "XMLFile.xml")
         GridView1.DataSource = oDs
     End If
End Sub

And here is the content of the XML file. We want to add the price of each product to a variable called dTotal and then display this value in the GridView footer.

<?xml version="1.0" encoding="utf-8" ?>
    <name>Camembert Pierrot</name>
    <name>Scottish Longbreads</name>
    <name>Rhönbräu Klosterbier</name>

The Code

Insert an event handler for the RowCreated event into your code-behind file. Then, check the RowType of each new row. In the case of DataControlRowType.DataRow, add the value of "price" to the variable dTotal.

In the case of DataControlRowType.Footer, assign the value of dTotal to the third cell of the GridView footer.

Dim dTotal As Decimal = 0
Protected Sub GridView1_RowDataBound(ByVal sender As Object, _
          ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
          Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        dTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "price"))
    End If
    If e.Row.RowType = DataControlRowType.Footer Then
        e.Row.Cells(1).Text = "Totals:"
        e.Row.Cells(2).Text = dTotal.ToString("c")
        e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Right
        e.Row.Cells(2).HorizontalAlign = HorizontalAlign.Right
        e.Row.Font.Bold = True
    End If
End Sub

To display the footer, you have to set the attribute ShowFooter="True".


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Germany Germany
Florian works as consultant for change- and configuration management for about 7 years. In this environment he is often forced to work with unix, perl and shell scripts.

For more information about change- and configuration management (espacially Serena Dimensions) visit:

For video tutorials about, ajax, gridviews, ... (in german) visit:

You may also be interested in...

Comments and Discussions

General[My vote of 1] this should be a tip - not an article... Pin
Seishin#13-Nov-10 9:03
memberSeishin#13-Nov-10 9:03 
Generaltotal when using SQL datasource Pin
NidaNovice26-Jan-09 19:23
memberNidaNovice26-Jan-09 19:23 
GeneralPaginated GridView Pin
Alex.Shnaider7-Dec-08 1:03
memberAlex.Shnaider7-Dec-08 1:03 
GeneralThanks. Very useful Pin
Member 40568378-Apr-08 13:39
memberMember 40568378-Apr-08 13:39 
Generalthanx its helpfull. [modified] Pin
Minhajkk5-Sep-06 1:03
memberMinhajkk5-Sep-06 1:03 
GeneralRe: thanx its helpfull. Pin
vik205-Sep-06 19:53
membervik205-Sep-06 19:53 
AnswerRe: thanx its helpfull. Pin
Minhajkk5-Sep-06 20:32
memberMinhajkk5-Sep-06 20:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 5 Sep 2006
Article Copyright 2006 by fstrahberger
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid