Click here to Skip to main content
16,020,261 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,

I have created a gridview dynamically. There is template fields described in designing portion. All the columns were created thru code behind as follows. Its works fine. Here I can listed the pages for each rows. But I dont know how to implement the sum of pages in the footer template thru code behind.


C#
TemplateField Pages = new TemplateField();
Pages.HeaderText = "Pages";
Pages.ItemTemplate = new GridViewTemplate_Pages();
gv1.Columns.Add(Pages);


        public class GridViewTemplate_Pages : ITemplate
        {
            
            void ITemplate.InstantiateIn(Control container)
            {
                Label PagesLabel = new Label();
                PagesLabel.DataBinding += new EventHandler(this.PagesLabel_DataBinding);
                container.Controls.Add(PagesLabel);
            }

            void PagesLabel_DataBinding(object sender, EventArgs e)
            {
                Label lbl1 = (Label)sender;
                GridViewRow row = (GridViewRow)lbl1.NamingContainer;
                lbl1.Text = DataBinder.Eval(row.DataItem, "PagesReceived").ToString();
            }
        }
Given ShowFooter="True" in aspx page and RowDataBound written separately. The following code works fine if I given footer template in aspx page but do not know how to get the result in programmatically. Please advice.

C#
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int RowTotalPages = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PagesReceived"));
                TotalPages = TotalPages + RowTotalPages;
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label m = (Label)e.Row.FindControl("gv1TotalPages");
                m.Text = TotalPages.ToString();
            }
        }
Posted

1 solution

Its a silly thing. I made it complex. I need the sum of pages in the footer row, then why should I think and confuse about GridViewTemplate and Itemplate class. Simply I added the Total pages in the session and recollect it in the DataControlRowType.Footer. Here is the code:

C#
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               int RowTotalPages = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PagesReceived"));
               TotalPages = TotalPages + RowTotalPages;
                Session.Add("TotalPages", TotalPages.ToString());
            }
           if (e.Row.RowType == DataControlRowType.Footer)
           {
               e.Row.Cells[4].Text = "Batches Count: " + gv1.Rows.Count.ToString();
               e.Row.Cells[5].Text = Session["TotalPages"].ToString();
            }
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900