?
Solved

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

Posted on 2008-11-12
2
Medium Priority
?
1,822 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:CharlieDev
2 Comments
 
LVL 4

Accepted Solution

by:
ARahimAwan earned 2000 total points
ID: 22938496
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
 

Author Closing Comment

by:CharlieDev
ID: 31515883
Brilliant!! Thanks thats solved the problem
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

830 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