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

How to display a totals line in a GridView footer

By , 4 Sep 2006
 

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)

About the Author

fstrahberger
Web Developer
Germany Germany
Member
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
General[My vote of 1] this should be a tip - not an article...memberSeishin#13 Nov '10 - 9:03 
because it's not an article...
so are your other "articles"...
life is study!!!

Generaltotal when using SQL datasourcememberNidaNovice26 Jan '09 - 19:23 
hi my grid view is connected to database using sql data source..the gridview data is controlled by dropdown list selected item..now i want footer to display total. and then use the total in a variable....the data is changing so there isnt any fixed xml file or dataset....
i've used the rowdatabound event code and it doesnt give any result please somebody help!!!
GeneralPaginated GridViewmemberAlex.Shnaider7 Dec '08 - 1:03 
If you have paginated gridview, then displayed totals are for current page
Do you have solution for displaying totals for all the report
GeneralThanks. Very usefulmemberMember 40568378 Apr '08 - 13:39 
Thanks, this is an awesome post. It is simple and easy to implement.
Really cool of you to post this.
 
Oscar Valles
 
VRCGI

Generalthanx its helpfull. [modified]memberMinhajkk5 Sep '06 - 1:03 
its really helpfull here is C# version.
 

decimal dTotal = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
dTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "price"));
}
if (e.Row.RowType == DataControlRowType.Footer)
{
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;
}
}

GeneralRe: thanx its helpfull.membervik205 Sep '06 - 19:53 
Good work Many people face prb using the vb work in C#
 
vikram
http://www.vikramlakhotia.com/

AnswerRe: thanx its helpfull.memberMinhajkk5 Sep '06 - 20:32 
Welcome.

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

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