asp.net gridview with footer-how to delete an unwanted column

Hi,
I am trying to remove the last column on the gridview on my asp.net page. Since I've added some c# code to add up the sum of the columns another column has appeared on the gridview table which i cant get rid of.

Anyone know what i need to change?
Thanks
asp.net 
 
 <asp:GridView ID="gvwTimesheet" runat="server" AutoGenerateColumns="False" CssClass="gridTimesheet" ShowFooter="true"
                                 onRowDataBound="sumRows" onRowCreated="setFooter">
                                
                                   
                    <columns>
                        <asp:BoundField DataField="ClientName" HeaderText="Client name" />
                        <asp:BoundField DataField="ProjectName" HeaderText="Project name" />
                        <asp:BoundField DataField="EmployeeName" HeaderText="Employee name" />
                        <asp:BoundField DataField="Date" HeaderText="Date:" />
                       
                        <asp:templatefield headertext="Task">
                        <itemtemplate>
                            <%# Eval("Task") %>
                            <span class="tableLinks">
                            | <a href="EditTimesheet.aspx?ID=<%# Eval("ID") %>">Edit</a> 
                            </span>
                        </itemtemplate> 
                        </asp:templatefield>
                        <asp:BoundField DataField="Timespent" HeaderText="Timespent:" />
                        
            
                    </columns>
                </asp:GridView>
 
 
 
 
c#
 
 public void sumRows(Object src, GridViewRowEventArgs e)
    {
       
 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TableCellCollection cells = e.Row.Cells;
            total += Int32.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;
            
        }
 
 
    }

Open in new window

screenshottable.jpg
CharlieDevAsked:
Who is Participating?
 
ARahimAwanConnect With a Mentor Commented:
Problem is in setFooter Method. You need to Remove 4 columns and then set colspan of first column to 5. Following would work fine.

 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;
        }
    }
0
 
CharlieDevAuthor Commented:
Brilliant!! Thanks thats solved the problem
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.