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:

<?xml version="1.0"?>
<configuration>

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

</configuration>

Design:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<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;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<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">

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

<br /><br />
<br /><br />
All rights reserved by <a href="http://www.hightechnology.in">www.Hightechnology.in</a> |
Hosting partner <a href="http://www.grootstech.com" target="_blank">Grootstech</a>
</div>
</form>
</body>
</html>

Code:

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();
        }
    }
}

Result:

Total of Columns in GridView Footer in Asp.Net