Asp.Net GridView summary on Footer

In the scope of Asp.Net GridView, the need to present summary data arises frequently, particularly when generating reports. A key requirement in this regard is to display the total value derived from all rows within a dedicated Footer row. Fortunately, incorporating this vital footer row into the GridView is a seamless process.

Footer template

Developers can tap into the GridView control's inherent capabilities to effortlessly organize and manipulate tabular data. utilizing functionalities such as the footer template, they can precisely reflect aggregated values derived from the grid rows. This integration of the footer row not only enhances the visual appeal of the GridView but also provides users with a concise summary of the data.

By incorporating a footer row, the GridView imparts a streamlined and structured appearance, enabling users to navigate through the data more efficiently. This feature empowers users to gain a comprehensive understanding of the information presented, facilitating better analysis and decision-making.

Through the use of the footer template and other related functionalities, developers can elevate the usability and visual presentation of the GridView, ensuring a more engaging and informative experience for users.

Database

In this article, we have utilized Microsoft's Pubs database as a reliable source of sample data. To access this invaluable resource, you can easily download it free of charge by following the provided link.

Download

Calculate Total and display in Gridview

Within the upcoming program, the primary objective entails retrieving data from the STOR table within the esteemed PUBS database and subsequently showcasing the Total Quantity within the footer row. To successfully accomplish this, the inclusion of a FooterTemplate within the GridView becomes imperative as it enables the generation of summary reports in an efficient and organized manner.

gridview total on footer

Add gridview totals in the footer

In the code behind, during the RowDataBound event, a crucial operation takes place wherein the quantity is fetched from each individual row, subsequently leading to the calculation of the total quantity.

gridview footer

Once the total quantity has been accurately calculated, the program proceeds to assign this value to the lblTotalqty control located within the footer of the GridView.

GridView summary on Footer

gridview summary on footer Default.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="true" onrowdatabound="GridView1_RowDataBound"> <Columns> <asp:BoundField DataField="ord_num" HeaderText="ord_num" /> <asp:BoundField DataField="stor_id" HeaderText="stor_id" /> <asp:BoundField DataField="title_id" HeaderText="title_id" /> <asp:BoundField DataField="payterms" HeaderText="payterms" /> <asp:TemplateField HeaderText="Quantity"> <ItemTemplate> <div style="text-align: right;"> <asp:Label ID="lblqty" runat="server" Text='<%# Eval("qty") %>' /> </div> </ItemTemplate> <FooterTemplate> <div style="text-align: right;"> <asp:Label ID="lblTotalqty" runat="server" /> </div> </FooterTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="#cccccc" Font-Bold="True" ForeColor="Black" HorizontalAlign="Left" /> </asp:GridView> </div> </form> </body> </html>
C# Source Code
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; public partial class _Default : System.Web.UI.Page { int total = 0; protected void Page_Load(object sender, EventArgs e) { SqlDataAdapter adapter = new SqlDataAdapter(); DataSet ds = new DataSet(); int i = 0; string sql = null; string connetionString = "Data Source=.;Initial Catalog=pubs;User ID=sa;Password=*****"; sql = "select top 6 ord_num,stor_id,title_id,payterms,qty from sales"; SqlConnection connection = new SqlConnection(connetionString); connection.Open(); SqlCommand command = new SqlCommand(sql, connection); adapter.SelectCommand = command; adapter.Fill(ds); adapter.Dispose(); command.Dispose(); connection.Close(); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Label lblqy = (Label)e.Row.FindControl("lblqty"); int qty = Int32.Parse(lblqy.Text ); total = total + qty; } if (e.Row.RowType == DataControlRowType.Footer) { Label lblTotalqty = (Label)e.Row.FindControl("lblTotalqty"); lblTotalqty.Text = total.ToString(); } } }
VB.Net Source Code
Imports System.Drawing Imports System.Data.SqlClient Imports System.Data Partial Class _Default Inherits System.Web.UI.Page Dim total As Integer = 0 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim adapter As New SqlDataAdapter() Dim ds As New DataSet() Dim i As Integer = 0 Dim sql As String = Nothing Dim connetionString As String = "Data Source=.;Initial Catalog=pubs;User ID=sa;Password=*****" sql = "select top 6 ord_num,stor_id,title_id,payterms,qty from sales" Dim connection As New SqlConnection(connetionString) connection.Open() Dim command As New SqlCommand(sql, connection) adapter.SelectCommand = command adapter.Fill(ds) adapter.Dispose() command.Dispose() connection.Close() GridView1.DataSource = ds.Tables(0) GridView1.DataBind() End Sub Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then Dim lblqy As Label = DirectCast(e.Row.FindControl("lblqty"), Label) Dim qty As Integer = Int32.Parse(lblqy.Text) total = total + qty End If If e.Row.RowType = DataControlRowType.Footer Then Dim lblTotalqty As Label = DirectCast(e.Row.FindControl("lblTotalqty"), Label) lblTotalqty.Text = total.ToString() End If End Sub End Class