This is a migrated thread and some comments may be shown as answers.

Header fo Column-Header - Aggregate?

1 Answer 70 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Milan Gurung
Top achievements
Rank 1
Milan Gurung asked on 02 Jan 2009, 11:45 AM
Hi,

I came across an article that helps to format the grid - Header Grouping. It is accessible from Customizing with GridTemplateColumn  . Now my problem - I have got a numerical column within the GridTemplateColumn. Is it possible to aggregate the values present in that column and display it in its own footer.

Output should be

Col1    Col2
   X    Y
A        x1    10
B        x2     0
C       x3      20
  total: 30

On the above grid, col2 is divided into X an Y sub columns. I need aggregation of Y column.

Any sort of help is highly appreciated.

Thanks a lot.

Milan G

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 05 Jan 2009, 06:13 AM
Hello Milan,

You can refer to the following code to achieve the required scenario.
aspx:
<telerik:GridTemplateColumn  UniqueName="TemplateCol" HeaderText="Template Column">             
            <ItemTemplate>    
              <TABLE id="Table1" cellSpacing="1"  runat="server" cellPadding="1" width="300" border="1"
                  <TR> 
                      <TD id="td1" width="50%"><%# DataBinder.Eval(Container.DataItem, "ProductName")%></TD
                      <TD id="td2" width="50%"><%# DataBinder.Eval(Container.DataItem, "ProductID")%></TD
                  </TR> 
              </TABLE>                 
            </ItemTemplate> 
 
            <FooterTemplate> 
                   <asp:Label ID="Label" runat="server" Text="Label"></asp:Label> 
            </FooterTemplate> 
</telerik:GridTemplateColumn> 

cs:
private double sum = 0
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    {      
 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem data = (GridDataItem)e.Item; 
            HtmlTable tble = (HtmlTable)data["TemplateCol"].FindControl("Table1"); 
            HtmlTableCell tblecell = (HtmlTableCell)tble.Controls[0].FindControl("td2"); 
            string text = tblecell.InnerText; 
            sum += double.Parse(text);          
        } 
 
       if (e.Item is GridFooterItem) 
        { 
            GridFooterItem footer = (GridFooterItem)e.Item;          
            (footer["TemplateCol"].FindControl("Label") as Label).Text = sum.ToString(); 
        } 
   } 

Note: To access the Html table in the code behind, set the runat attribute as server.
Hope this helps..
Thanks
Princy
Tags
Grid
Asked by
Milan Gurung
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or