Total of Columns in GridView Footer in Asp.Net

Total of Columns in GridView Footer in Asp.Net

In this tutorial we will learn Total of Columns in GridView Footer in Asp.Net. In this we have a gridview and we have to display sum of column in the footer row. Here we have used Adventureworks database for demo purpose, table used in this demo is Purchasing.PurchaseOrderDetail. You can download Adventureworks database from following link. Link:

I have used gridview row databound condition for this functionality will work for paging enabled gridview also.

After that set your database connection in web.config like this because we are using this connection in our sqldatasource to get the data from database.


<?xml version="1.0"?>

      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    <add name="con" connectionString="Data Source=.;;Integrated Security=true;Initial Catalog=AdventureWorks2008R2"/>



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
<title>Total of Columns in GridView Footer in Asp.Net</title>
<style type="text/css">
body {
width: 980px;
margin: 0px auto;
text-align: center;
padding-top: 50px;
font-size: 20px;
<form id="form1" runat="server">
<h1>Total of Columns in GridView Footer in Asp.Net</h1>   
<asp:GridView ID="gv" runat="server" AllowPaging="true" PageSize="5" ShowFooter="true" AutoGenerateColumns="false" 
    Width="100%" DataSourceID="sdsrc" OnRowDataBound="gv_RowDataBound">

<asp:BoundField DataField="ProductID" HeaderText="Product ID" />
<asp:BoundField DataField="PurchaseOrderDetailID" HeaderText="Purchase Order ID" />
<asp:TemplateField HeaderText="Order Quantity">
<asp:Label ID="lblOrderQty" runat="server" Text='<%#Eval("OrderQty") %>' />
<asp:Label ID="lbltxtTotal" runat="server" Text="Total Price" />
<asp:TemplateField HeaderText="Price">
<asp:Label ID="lblPrice" runat="server" />
<asp:Label ID="lblTotal" runat="server" />
<asp:SqlDataSource ID="sdsrc" runat="server" SelectCommand="select * from [AdventureWorks2008R2].[Purchasing].[PurchaseOrderDetail]" ConnectionString="<%$ ConnectionStrings:con %>">

<br /><br />
<br /><br />
All rights reserved by <a href=""></a> |
Hosting partner <a href="" target="_blank">Grootstech</a>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    decimal total = 0;
    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            Label lblPrice = (Label)e.Row.FindControl("lblPrice");
            decimal price = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "UnitPrice")) * Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "OrderQty"));
            lblPrice.Text = price.ToString();
            total += price;
        if (e.Row.RowType == DataControlRowType.Footer)
            Label lblTotal = (Label)e.Row.FindControl("lblTotal");
            lblTotal.Text = total.ToString();


Total of Columns in GridView Footer in Asp.Net