How to Display Sum Total in the Footer of the GridView Control ?


Hi

Here are some few step to perform this task

Step1: Take one gridview and Sqldatasource control in default.aspx page

step2:  Configure the SqldataSource control and gridview control like this

<div>
<asp:GridView ID=”GridView1″  DataKeyNames=”Id” ShowFooter=”true”
runat=”server” AutoGenerateColumns=”False”
DataSourceID=”SqlDataSource1″ onrowdatabound=”GridView1_RowDataBound” >
<Columns>
<asp:BoundField DataField=”Id” HeaderText=”Id” InsertVisible=”False”
ReadOnly=”True” SortExpression=”Id” />
<asp:BoundField DataField=”EmpName” HeaderText=”EmpName”
SortExpression=”EmpName” />
<asp:BoundField DataField=”EmpSal” HeaderText=”EmpSal”
SortExpression=”EmpSal” />

</Columns>
</asp:GridView>

<br />
<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server”
ConnectionString=”<%$ ConnectionStrings:ConnectionString %>”
SelectCommand=”SELECT * FROM [tblEmp]”></asp:SqlDataSource>

</div>

Step3: Click on “RowDataBound” event of gridview like this

Step 4: Write the code like This

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

public partial class _Default : System.Web.UI.Page
{
private decimal sum = 0;
protected void Page_Load(object sender, EventArgs e)
{

}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
sum = sum + Convert.ToDecimal(e.Row.Cells[2].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = “Total Rs”;
//e.Row.Cells[2].Text = sum.ToString(“c”, new CultureInfo(“hi-IN”));
e.Row.Cells[2].Text = sum.ToString();
e.Row.Font.Bold = true;
}

}
}

Step 5: Compile the code .You will Output like this

I hope this will help to some one.

Advertisement

7 thoughts on “How to Display Sum Total in the Footer of the GridView Control ?

  1. wonderful publish, very informative. I’m wondering why the other specialists of this sector don’t understand this.
    You should continue your writing. I am confident,
    you have a huge readers’ base already!

    • Chandra Dev December 18, 2012 / 10:24 am

      Hi

      Thanks for sending kind and motivative feedback. I will continue my writing.

  2. visit website December 24, 2012 / 10:34 am

    What’s Happening i’m new to this, I stumbled upon this I
    have discovered It positively useful and it has aided
    me out loads. I hope to give a contribution & help other users like its aided me.
    Great job.

  3. Fillers Vadnais Heights July 25, 2013 / 8:03 am

    I don’t know whether it’s just me or if perhaps everyone else encountering issues with your blog.

    It seems like some of the written text on your posts are running off the
    screen. Can somebody else please comment and let
    me know if this is happening to them too? This may be a problem with my internet browser because I’ve had this happen before. Kudos

  4. Hydrafacial Minneapolis August 11, 2013 / 12:53 pm

    Thanks a lot for sharing this with all of us you actually know what
    you are speaking approximately! Bookmarked. Please also visit my web
    site =). We can have a hyperlink alternate
    arrangement among us

  5. clintonmutch October 3, 2014 / 3:24 pm

    Very good post. I am facing a few of these issues as well..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.