[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2805
  • Last Modified:

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

0
CharlieDev
Asked:
CharlieDev
  • 4
1 Solution
 
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
 
CharlieDevAuthor 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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now