c# asp.net- gridview footer with SUM function

Hi,
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
Thanks
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.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

CharlieDevAsked:
Who is Participating?
 
CharlieDevConnect With a Mentor Author Commented:
Incase anyone reads this , the format is: string.Format("{0:n2}", total);

this link is very helpful for formatting syntax:
http://blogs.msdn.com/kathykam/archive/2006/03/29/564426.aspx
0
 
CharlieDevAuthor Commented:
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?
0
 
CharlieDevAuthor Commented:
My formatting of the number is wrong string.Format("{0:n}", total); as I now have a larger number, 1,100
0
 
CharlieDevAuthor Commented:
Anyone know what I use in the format bit to allow for longer decimals instead of the following code?
string.Format("{0:n}", total);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.