GridView Footer Count


In this article I'm trying to explain how to show total count in GridView footer. for this i just calculate the employee total salary and display that in footer of GridView. In future they may consider this total amount for records purpose.

Total in GridView Footer:


In this article I'm trying to explain how to show total count in GridView footer. For this we must set the GridView ShowFooter property to be true.

Description:


In this article I'm trying to explain how to show total count in GridView footer. For this we must set the GridView ShowFooter property to be true, and in RowDataBoundEvent calculate the footer count and assign that count to footer template.

<asp:GridView ID="GV" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" AutoGenerateColumns="false" OnRowDataBound="GV_OnRowDataBound" ShowFooter="true">

Design footer wherever you need to show total in gridview like below


<FooterTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</FooterTemplate>


Create Procedure:





/*
EXEC Get_Employees @DeptNo=1
*/
CREATE PROCEDURE Get_Employees
(
@DeptNo INT
)
AS
BEGIN
SELECT *
FROM EMP
WHERE DEPTNO=@DeptNo
END

This procedure we can call in our Application in the below manner.

Source Code:




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

<!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>
<style type="text/css">
.style1
{
color: #000066;
font-size: large;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div align="center">

<strong><span class="style1">GridView Footer Count</span><br />
<br />
</strong>
<asp:GridView ID="GV" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" AutoGenerateColumns="false" OnRowDataBound="GV_OnRowDataBound" ShowFooter="true">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />

<Columns>
<asp:TemplateField HeaderText="Employee Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"ENAME") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Job Description">
<ItemTemplate>
<asp:Label ID="lblJob" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"JOB") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<ItemTemplate>
<asp:Label ID="lblSal" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"SAL") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

</div>
</form>
</body>
</html>


Code Behind:




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 GridFooter : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("DataBase=ENGSOFT;User id=sa;Password=P@ssword9");
SqlDataAdapter da;
DataSet ds;
DataTable dt;
SqlCommand cmd;
int DeptNo=1;
double Total = 0;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind_GV();
}
}
protected void Bind_GV()
{
cmd = new SqlCommand("Get_Employees", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@DeptNo", DeptNo);
dt = new DataTable();

try
{
con.Open();
da = new SqlDataAdapter(cmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
GV.DataSource = dt;
GV.DataBind();
}
else
{
DataRow dr = dt.NewRow();

dt.Rows.Add(dr);
dt.AcceptChanges();
GV.DataSource = dt;
GV.DataBind();
GV.Rows[0].Visible = false;
}
}
catch (Exception ex)
{

}
finally
{
con.Close();
con.Dispose();
}
}
protected void GV_OnRowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
double Sal =Convert.ToDouble(((Label)e.Row.FindControl("lblSal")).Text);

Total = Total + Sal;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
((Label)e.Row.FindControl("lblTotal")).Text = "Total :" + Total;
}
}
}

Output:



GV Footer

Conclusion:


Using this we can easily calculate the total and display in gridview. Hope this will help you those who are searching for the same.


Attachments

  • Source Code (45189-221610-Source-Code.zip)
  • Article by naveensanagasetti
    I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

    Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

    Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: