Thursday, October 8, 2009

Show grand total running total in GridView Footer

Please visit my new Web Site

In many scenarios we need to display Running total as well as Grand total in GridView footer. In this post i will try to explain in an easy way that how we can display running total & Grand total in a GridView footer combindly. To explain this solution using an example here i use a sales order report. The report contains all sales order amount in a tabular way. I will use a GridView to display sales order amount and use GridView footer to display Running total & Grand total. Let we have a customer table with id and name column plus an orders table with OrderID,CustomerID,OrderDate & Amount. Now our goal is to show all customers order with amount as well as page wise running total & grand total. Note that running total is necessary when you enable paging in a GridView where as Grand total is always you can consider.

The output will be:

Fig: Page 1 runnig total

When you click on another page within GridView then you will get that page Running total with Grand total also.

Fig: Page 2 runnig total

To accomplish the above example please find the HTML markup code for your aspx page from below:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gvgrandtotl.aspx.cs" Inherits="gvgrandtotl" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >
<head runat="server">


<form id="form1" runat="server">


<asp:GridView runat="server" ID="gvEdit" AllowPaging="true" PageSize="5" ShowFooter="true" OnPageIndexChanging="gvEdit_PageIndexChanging" OnRowDataBound="gvEdit_RowDataBound">
<asp:BoundField DataField="Code" HeaderText="Order Code">
<asp:BoundField DataField="Name" HeaderText="Name">

<asp:TemplateField HeaderText="Date" FooterStyle-BorderWidth="1px" FooterStyle-BorderColor="maroon">
<asp:Label ID="lblDate" runat="server" Text='<%# Convert.ToDateTime(Eval("Date")).ToString("dd-MM-yy")%>'></asp:Label>
<asp:Label ID="lbltxtRTotal" runat="server">Running Total:</asp:Label><br />
<asp:Label ID="Label1" runat="server">Grand Total:</asp:Label><br />

<asp:TemplateField HeaderText="Amount" FooterStyle-BorderWidth="1px" FooterStyle-BorderColor="maroon">
<asp:Label ID="lblAmount" runat="server" Text='<%# Eval("Amount").ToString()%>'></asp:Label>
<asp:Label ID="lblRTotal" runat="server"></asp:Label><br />
<asp:Label ID="lblGTotal" runat="server"></asp:Label>




To accomplish the above example please find the server side code for your aspx page from below:
using System;
using System.Data;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class gvgrandtotl : System.Web.UI.Page

decimal RunningTotal = 0;
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
string connectionString = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(connectionString);
using (conn)
SqlDataAdapter ad = new SqlDataAdapter(
"SELECT OrderID Code,B.Name,OrderDate Date,Amount from "+
"orders A INNER JOIN customer B ON A.Customerid=B.ID", conn);

decimal GrandTotal = 0;
foreach (DataRow oRow in dt.Rows)
GrandTotal += Convert.ToDecimal(oRow["Amount"]);

ViewState["GrandTotal"] = GrandTotal;

gvEdit.DataSource = dt;
ViewState["dt"] = dt;

protected void gvEdit_PageIndexChanging(object sender, GridViewPageEventArgs e)
gvEdit.PageIndex = e.NewPageIndex;
gvEdit.DataSource = (DataTable)ViewState["dt"];

protected void gvEdit_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
RunningTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Amount"));

if (e.Row.RowType == DataControlRowType.Footer)
((Label)e.Row.FindControl("lblRTotal")).Text = RunningTotal.ToString();
((Label)e.Row.FindControl("lblGTotal")).Text = ViewState["GrandTotal"].ToString();

Code explanation:
If you look at the page load event where i bind the datasource to the GridView control then i use a viewstate variable to store the retrieved sql server data because i do not go to the database server again when user click on another page of the gridview. At that time i also calculate Grand total value from the datatable & stored it in viewstate which i use in GridView RowDataBound event.

When i assign the datasource property to the GridView control then the RowDataBound event automatically fired. In this event i need to check that does the DataControl Row Type is DataRow or not. If its a DataRow that means i found the DataItem object which will be currently rendered in our page. So by accumulating the amount value will give me the Running Total and Grnad total value we have calculated previously in page load event. So now check the DataControl Row Type for Footer row. If you find the footer row then you knew that in Fotter row we have placed two label named lblRTotal and lblGTotal. Now by using the FindControl method you can catch both labels. Now just put the running total in lblRTotal Text property and Grand total in lblGTotal label Text property. That's it.


Anonymous said...


How do i handle the same situation if i have a dataset instead of datatable,

Please guide me urgently

i will be happy to here you soon on

Kripal Sinh Jadeja

Anonymous said...

Thank you for the posting...Havent tried your codes yet, but twas really comforting that this need of mine has a solution... Keep it up...

Anonymous said...

The solution above work fine..... what if i have multiple columns please advise

Shawpnendu said...

Same brother. Just try to understand my logic.

Anonymous said...

thanks sorted it out

Rajesh Kumar Patel said...

Thanks, It helps me a lot

Want to say something?

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash