Click here to Skip to main content
15,867,835 members
Articles / Web Development / ASP.NET

How to display a totals line in a GridView footer

Rate me:
Please Sign up or sign in to vote.
3.13/5 (8 votes)
4 Sep 2006CPOL 74K   374   32   7
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:

ASP.NET
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
<?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.

ASP.NET
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)


Written By
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... Pin
Seishin#13-Nov-10 9:03
Seishin#13-Nov-10 9:03 
Generaltotal when using SQL datasource Pin
NidaNovice26-Jan-09 19:23
NidaNovice26-Jan-09 19:23 
GeneralPaginated GridView Pin
Alex.Shnaider7-Dec-08 1:03
Alex.Shnaider7-Dec-08 1:03 
GeneralThanks. Very useful Pin
oscarvalles8-Apr-08 13:39
oscarvalles8-Apr-08 13:39 
Generalthanx its helpfull. [modified] Pin
mehmoodmi5-Sep-06 1:03
mehmoodmi5-Sep-06 1:03 
GeneralRe: thanx its helpfull. Pin
vik205-Sep-06 19:53
vik205-Sep-06 19:53 
AnswerRe: thanx its helpfull. Pin
mehmoodmi5-Sep-06 20:32
mehmoodmi5-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.