Categories
Asp, Asp.net

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: http://msftdbprodsamples.codeplex.com/releases/view/93587

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.

Web.config:

01<?xml version="1.0"?>
02<configuration>
03 
04    <system.web>
05      <compilation debug="true" targetFramework="4.5" />
06      <httpRuntime targetFramework="4.5" />
07    </system.web>
08  <connectionStrings>
09    <add name="con" connectionString="Data Source=.;;Integrated Security=true;Initial Catalog=AdventureWorks2008R2"/>
10  </connectionStrings>
11 
12</configuration>

Design:

01<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
02 
03<!DOCTYPE html>
04 
06<head runat="server">
07<title>Total of Columns in GridView Footer in Asp.Net</title>
08<style type="text/css">
09body {
10width: 980px;
11margin: 0px auto;
12text-align: center;
13padding-top: 50px;
14font-size: 20px;
15}
16</style>
17</head>
18<body>
19<form id="form1" runat="server">
20<div>
21<h1>Total of Columns in GridView Footer in Asp.Net</h1>  
22<asp:GridView ID="gv" runat="server" AllowPaging="true" PageSize="5" ShowFooter="true" AutoGenerateColumns="false"
23    Width="100%" DataSourceID="sdsrc" OnRowDataBound="gv_RowDataBound">
24 
25<Columns>
26<asp:BoundField DataField="ProductID" HeaderText="Product ID" />
27<asp:BoundField DataField="PurchaseOrderDetailID" HeaderText="Purchase Order ID" />
28<asp:TemplateField HeaderText="Order Quantity">
29<ItemTemplate>
30<asp:Label ID="lblOrderQty" runat="server" Text='<%#Eval("OrderQty") %>' />
31</ItemTemplate>
32<FooterTemplate>
33<asp:Label ID="lbltxtTotal" runat="server" Text="Total Price" />
34</FooterTemplate>
35</asp:TemplateField>
36<asp:TemplateField HeaderText="Price">
37<ItemTemplate>
38<asp:Label ID="lblPrice" runat="server" />
39</ItemTemplate>
40<FooterTemplate>
41<asp:Label ID="lblTotal" runat="server" />
42</FooterTemplate>
43</asp:TemplateField>
44</Columns>
45</asp:GridView>
46      
47<asp:SqlDataSource ID="sdsrc" runat="server" SelectCommand="select * from [AdventureWorks2008R2].[Purchasing].[PurchaseOrderDetail]" ConnectionString="<%$ ConnectionStrings:con %>">
48</asp:SqlDataSource>
49 
50<br /><br />
51<br /><br />
52All rights reserved by <a href="http://www.hightechnology.in">www.Hightechnology.in</a> |
53Hosting partner <a href="http://www.grootstech.com" target="_blank">Grootstech</a>
54</div>
55</form>
56</body>
57</html>

Code:

01using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Web;
05using System.Web.UI;
06using System.Web.UI.WebControls;
07 
08public partial class _Default : System.Web.UI.Page
09{
10    protected void Page_Load(object sender, EventArgs e)
11    {
12 
13    }
14    decimal total = 0;
15    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
16    {
17        if (e.Row.RowType == DataControlRowType.DataRow)
18        {
19            Label lblPrice = (Label)e.Row.FindControl("lblPrice");
20            decimal price = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "UnitPrice")) * Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "OrderQty"));
21            lblPrice.Text = price.ToString();
22            total += price;
23        }
24        if (e.Row.RowType == DataControlRowType.Footer)
25        {
26            Label lblTotal = (Label)e.Row.FindControl("lblTotal");
27            lblTotal.Text = total.ToString();
28        }
29    }
30}

Result:

Total of Columns in GridView Footer in Asp.Net