C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET GridView »

Calculating the Total values on Footer


Posted Date: 26 Jun 2009    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: DIVYAMember Level: Silver    
Rating: 1 out of 5Points: 10



This will help to do how we can calculate the Total values on a Grid View control.
We can add the sum of both row wise and column wise.If any one is stuck in coding can refer this code..
This is just a sample

The Grid View will look like this

a b Total
============
10 1 11
1 1 2
============
11 2 13


For this Put labels and bind the value using Eval(),and allow Footer equals true.then using methods which is written in code behind we can calculate the column total and grand totals.

HTML Code
                                       
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" ShowFooter="true" >
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="a">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# CalculateATotal(DataBinder.Eval(Container.DataItem,"a").ToString()) %>'></asp:Label>
</ItemTemplate>
<FooterTemplate >
<asp:Label ID="Label12" runat="server" Text='<%# aTotal%>'></asp:Label>
</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="b">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# CalculateBTotal(DataBinder.Eval(Container.DataItem,"b").ToString()) %>'></asp:Label>
</ItemTemplate>
<FooterTemplate >
<asp:Label ID="Label13" runat="server" Text='<%# bTotal%>'></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="total">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# CalculateTotal(DataBinder.Eval(Container.DataItem,"a").ToString(), DataBinder.Eval(Container.DataItem,"b").ToString())%>'></asp:Label>
</ItemTemplate>
<FooterTemplate >
<asp:Label ID="Label11" runat="server" Text='<%# grandTotal%>'></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
Grand Total :<asp:Label ID="Label4" runat="server"></asp:Label>
</div>
</form>
</body>
</html>


Code Behind

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
public int grandTotal;
public int aTotal;
public int bTotal;
protected void Page_Load(object sender, EventArgs e)
{
//SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ToString());

SqlConnection con = new SqlConnection(ConnProviderString());
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter adpt = new SqlDataAdapter("select a,b from val",con );
adpt.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
public string ConnProviderString()
{

string connectionString = "server=bharathi-166;integrated security=true;database=Master;uid=sa;pwd=;";
return connectionString;
}
protected string CalculateTotal(string a, string b)
{
int total = Convert.ToInt16(a) + Convert.ToInt16(b);
grandTotal = grandTotal + total;
return total.ToString();
}
protected string CalculateATotal(string a)
{
aTotal = aTotal + Convert.ToInt16(a);
return a;
}
protected string CalculateBTotal(string b)
{
bTotal = bTotal + Convert.ToInt16(b);
return b;
}
}


Attachments

  • Grid View and Footer (29650-26429-Code in aspx page.txt)


  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    GridView Column values addings to find Grand Total  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Move all sides in a grid template columns Text boxes.
    Previous Resource: Add text box in gridview at runtime
    Return to Discussion Resource Index
    Post New Resource
    Category: ASP.NET GridView


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use