.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to display a Toatal amount or Salary as a Footer in the Gridview(ASP.NET) ?

Posted By:Syed Shakeer Hussain       Posted Date: May 21, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

In this Article I am going to explain you how to display a Toatal amount or Salary at the Footer of the Gridview.

First add one datagridevew on a webpage.Now i am going to retrive a data from a Table named 'emp'.This emp table has 3 columns
column_Name          DataType
---------------------------------------
id                          number
empname               varchar(50)
salary                     number

In Soucrce code .aspx page

create a Template in gridvew  to display Data as follows in between the and

<asp:GridView ID="GridView1" runat="server" Style="z-index: 100; left: 271px; position: absolute;

top: 68px" AutoGenerateColumns="False" Height="165px" ShowFooter="True" CellPadding="4"

ForeColor="Black" GridLines="Vertical" Width="229px" BackColor="White" BorderColor="#DEDFDE"

BorderStyle="None" BorderWidth="1px">

<Columns>

<asp:TemplateField HeaderText="ID">

<ItemTemplate>

<asp:Label ID="idlbl" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"id")%>'>asp:Label>

ItemTemplate>

asp:TemplateField>

<asp:TemplateField HeaderText="NAME">

<ItemTemplate>

<asp:Label ID="namelbl" runat="server" Text-Align="right" Text='<%#DataBinder.Eval(Container.DataItem,"empname")%>'>asp:Label>

ItemTemplate>

asp:TemplateField>

<asp:TemplateField HeaderText="SALARY">

<ItemTemplate>

<asp:Label ID="sal1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"salary")%>'>asp:Label>

ItemTemplate>

asp:TemplateField>

Columns>

<FooterStyle BackColor="#CCCC99" />

<RowStyle BackColor="#F7F7DE" />

<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />

<PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#F7F7DE" />

<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="White" />

asp:GridView>

write a code for display data in grdview:

in aspx.cs:

protected void Page_Load(object sender, EventArgs e)

{

SqlConnection conn = new SqlConnection("connectionstring");

conn.Open();

SqlDataAdapter ad = new SqlDataAdapter("select * from emp", conn);

DataSet ds = new DataSet();

ad.Fill(ds, "emp");

GridView1.DataSource = ds.Tables[0];

GridView1.DataBind();

}

Below is the simple output Image of a gridview:

Now we have calculate the Total Salary then have to dispaly Total Salary at the Bottom of the Gridview.By using is used for the Last Line of our OutPut. eg:

Last Line_outPut  

NOTE:If we are using footer template we have to set ShowFooter Property as True by selecting the datagrid.Default is False

In this Example I am going to calculate the sum of Salary column.This sum should be displayed  in the salary column LastLine(bottom of the salary column).

I am using a method name as gettotal().here i am calculating the total salarys of employes.

public int gettotal()

{

SqlConnection conn = new SqlConnection("connectionstring");

conn.Open();

SqlCommand cmd = new SqlCommand("SELECT SUM(salary) FROM emp", conn);

int totalsal = Convert.ToInt32(cmd.ExecuteScalar());

return totalsal;

}

Next we have to use this gettotal () method in

<FooterTemplate>
<%# gettotal()%>
FooterTemplate>

here  <%# gettotal()%> will cal the gettotal() in aspx.cs page

we have to write use this FooterTemplate where we want a lastline output. Here I am using this FooterTemplate in salary column.

eg:

<asp:TemplateField HeaderText="SALARY">

<ItemTemplate>

<asp:Label ID="sal1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"salary")%>'>asp:Label>

ItemTemplate>

<FooterTemplate>

<%# gettotal()%>

FooterTemplate>

asp:TemplateField>

See the output Image

The aspx.page will look like as follows:

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

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 id="Head1" runat="server">

<title>Untitled Pagetitle>

head>

<body>

<form id="form1" runat="server">

<div>

<asp:GridView ID="GridView1" runat="server" Style="z-index: 100; left: 271px; position: absolute;

top: 68px" AutoGenerateColumns="False" Height="165px" ShowFooter="True" CellPadding="4"

ForeColor="Black" GridLines="Vertical" Width="229px" BackColor="White" BorderColor="#DEDFDE"

BorderStyle="None" BorderWidth="1px">

<Columns>

<asp:TemplateField HeaderText="ID">

<ItemTemplate>

<asp:Label ID="idlbl" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"id")%>'>asp:Label>

ItemTemplate>

asp:TemplateField>

<asp:TemplateField HeaderText="NAME">

<ItemTemplate>

<asp:Label ID="namelbl" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"empname")%>'>asp:Label>

ItemTemplate>

<FooterTemplate>

<asp:Label ID="namelbl" runat="server" Text="Total">asp:Label>

FooterTemplate>

asp:TemplateField>

<asp:TemplateField HeaderText="SALARY">

<ItemTemplate>

<asp:Label ID="sal1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"salary")%>'>asp:Label>

ItemTemplate>

<FooterTemplate>

<%# gettotal()%>

FooterTemplate>

asp:TemplateField>

Columns>

<FooterStyle BackColor="#CCCC99" />

<RowStyle BackColor="#F7F7DE" />

<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />

<PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#F7F7DE" />

<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="White" />

asp:GridView>

  

div>

form>

body>

html>

the complete asp.cs page is:

protected void Page_Load(object sender, EventArgs e)

{

SqlConnection conn = new SqlConnection("connectionstring");

conn.Open();

SqlDataAdapter ad = new SqlDataAdapter("select * from emp", conn);

DataSet ds = new DataSet();

ad.Fill(ds, "emp");

GridView1.DataSource = ds.Tables[0];

GridView1.DataBind();

gettotal();

}

 

public int gettotal()

{

SqlConnection conn = new SqlConnection("connectionstring");

conn.Open();

SqlCommand cmd = new SqlCommand("SELECT SUM(salary) FROM emp", conn);

int totalsal = Convert.ToInt32(cmd.ExecuteScalar());

return totalsal;

}

Plz give me Feedback about this Article.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend