c# gridview footer with SUM function

Posted on 2009-02-24
Last Modified: 2012-05-06
I have an old project with a gridview and a footer that calculates the total of the 6th column, this all works. I now want to use this code to calculate the total of the 3rd column in a new project. i cant figure out where I told the old code to total the 6th column and so I cant get my the code to work on the new project as I have fewer columns. Well, i think that is what the problem is.
Can someone show me where and how I told the code to add up a particular column
float total;


    public void sumRows(Object src, GridViewRowEventArgs e)



        if (e.Row.RowType == DataControlRowType.DataRow)


            TableCellCollection cells = e.Row.Cells;

            total += Single.Parse(float.Parse(cells[cells.Count - 1].Text.Split(' ')[0]).ToString());

            cells[cells.Count - 1].Text = string.Format("{0:n}",

            float.Parse(cells[cells.Count - 1].Text.Split(' ')[0]));



    public void setFooter(Object src, GridViewRowEventArgs e)


        if (e.Row.RowType == DataControlRowType.Footer)


            TableCellCollection cells = e.Row.Cells;

            cells.RemoveAt(0); cells.RemoveAt(0);



            cells[0].ColumnSpan = 5;

            cells[0].Text = "Total";

            cells[1].Text = string.Format("{0:n}", total);

            cells[1].BackColor = System.Drawing.Color.Orange;




aspx file


 <asp:GridView ID="gvwTimesheet" runat="server" AutoGenerateColumns="False" CssClass="gridTimesheet" ShowFooter="true"

                                 onRowDataBound="sumRows" onRowCreated="setFooter">

Open in new window

Question by:CharlieDev

    Author Comment

    I've found where it gets the column number (   [cells.Count - 1]  )and its getting number 3 ok, but not working still? Anyone know why?

    Author Comment

    My formatting of the number is wrong string.Format("{0:n}", total); as I now have a larger number, 1,100

    Author Comment

    Anyone know what I use in the format bit to allow for longer decimals instead of the following code?
    string.Format("{0:n}", total);

    Accepted Solution

    Incase anyone reads this , the format is: string.Format("{0:n2}", total);

    this link is very helpful for formatting syntax:

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now