SiteMap  | About    

GridView summary on Footer

In Asp.Net GridView, displaying summary data is a common report requirement. In usual situations we have to display the total value of all gridrows in the Footer row. We can include this footer row in GridView in a simple way.

gridview summary on footer


In this article I have used Microsoft's Pubs database for sample data. You can download it free from the following link.


Calculate Total and display in Gridview

In the following program, retrieving the data from the STOR table from PUBS database and display the Total Quantity at the footer row. In order to get the summary reports on footer row, we have to include FooterTemplate in the GridView.

gridview total on footer

Add gridview totals in the footer

In the code behind, at the RowDataBound event fetching the quantity from each row and calculating the total quantity.

gridview footer

After calculating the total quantity, the program assign the value to the lblTotalqty at the gridview Footer.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
<form id="form1" runat="server">
	<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
	ShowFooter="true" onrowdatabound="GridView1_RowDataBound">
		  <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">
				<div style="text-align: right;">
				<asp:Label ID="lblqty" runat="server" Text='<%# Eval("qty") %>' />
				<div style="text-align: right;">
				<asp:Label ID="lblTotalqty" runat="server" />
		<FooterStyle BackColor="#cccccc" Font-Bold="True" ForeColor="Black" HorizontalAlign="Left" />

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);
		SqlCommand command = new SqlCommand(sql, connection);
		adapter.SelectCommand = command;
		GridView1.DataSource = ds.Tables[0];
	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)
		Dim command As New SqlCommand(sql, connection)
		adapter.SelectCommand = command
		GridView1.DataSource = ds.Tables(0)
	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 (C) 2018    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.