Click here to Skip to main content
Click here to Skip to main content

How to display a totals line in a GridView footer

, 4 Sep 2006 CPOL
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

Introduction

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
         GridView1.DataBind()
     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" ?>
<products>
  <product>
    <id>1</id>
    <name>Camembert Pierrot</name>
    <price>10,99</price>
  </product>
  <product>
    <id>2</id>
    <name>Scottish Longbreads</name>
    <price>15,80</price>
  </product>
  <product>
    <id>3</id>
    <name>Rhönbräu Klosterbier</name>
    <price>29,90</price>
  </product>
</products>

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".

License

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

Share

About the Author

fstrahberger
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: www.venco.de
 
For video tutorials about asp.net, ajax, gridviews, ... (in german) visit: www.siore.com

Comments and Discussions

 
General[My vote of 1] this should be a tip - not an article... PinmemberSeishin#13-Nov-10 10:03 
Generaltotal when using SQL datasource PinmemberNidaNovice26-Jan-09 20:23 
GeneralPaginated GridView PinmemberAlex.Shnaider7-Dec-08 2:03 
GeneralThanks. Very useful PinmemberMember 40568378-Apr-08 14:39 
Generalthanx its helpfull. [modified] PinmemberMinhajkk5-Sep-06 2:03 
GeneralRe: thanx its helpfull. Pinmembervik205-Sep-06 20:53 
AnswerRe: thanx its helpfull. PinmemberMinhajkk5-Sep-06 21:32 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 5 Sep 2006
Article Copyright 2006 by fstrahberger
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid