Telerik is a leading vendor of ASP.NET AJAX, ASP.NET MVC, Silverlight, WinForms and WPF controls and components, as well as .NET Reporting, .NET ORM , .NET CMS, Code Analysis, Mocking, Team Productivity and Automated Testing Tools. Building on its expertise in interface development and Microsoft technologies, Telerik helps customers build applications with unparalleled richness, responsiveness and interactivity. Telerik products help thousands of companies to be more productive and deliver reliable applications under budget and on time.
Version Q1 2012 released 04/11/2012
select
  • Rate / Review
    • Give your Feedback:

      Rate this demo:


Grid / Grid Group Header and Footer Templates

UnitsInStock  
 Product nameUnits In StockUnit priceTotal Price
 Count : 77Sum : 3119Total price: 2222.71Total : 74050.85
Page size:
select
 77 items in 11 pages
Number of units: 0 Number of items in group: 5
 Chef Anton's Gumbo Mix021.350.00
 Alice Mutton039.000.00
 Thüringer Rostbratwurst0123.790.00
 Gorgonzola Telino012.500.00
 Perth Pasties032.800.00
  Product count: 5      Total units: 0      Total sum: 229.44      Total price: 0
Number of units: 3 Number of items in group: 1
 Sir Rodney's Scones310.0030.00
  Product count: 1      Total units: 3      Total sum: 10.00      Total price: 30
Number of units: 4 Number of items in group: 2 (Showing 1 of 2 items. Group continues on the next page.)
 Louisiana Hot Spiced Okra417.0068.00
  Product count: 1      Total units: 4      Total sum: 17.00      Total price: 68

  • Each GridTableView object contains a GroupHeaderTemplate and GroupFooterTemplate properties for specifying a template that will show inside each group's header and footer rows. You can use them to provide a more customized look for the group totals display.

    This demo shows how you can control their look based on the GroupIndex property of the header and footer objects. The main points are:

    • Use the AggregatesValues collection of the GridGroupHeaderItem to determine which is the field that the current group is based on and to display the relevant total aggregate value.
    • Use a binding expression to access the group footer totals.
    • Base the formatting of the header and footer templates on the GroupIndex which shows at what level of RadGrid's grouping is the current header/footer item.

Source Code

C# VB.NET
Show code in new window Demo isolation steps
  • <%@ Page Language="VB" CodeFile="DefaultVB.aspx.vb" Inherits="Telerik.GridExamplesCSharp.GroupBy.GroupHeaderFooterTemplates.DefaultVB" %>

    <%@ Register TagPrefix="telerik" Namespace="Telerik.QuickStart" %>
    <%@ Register TagPrefix="telerik" TagName="Header" Src="~/Common/Header.ascx" %>
    <%@ Register TagPrefix="telerik" TagName="HeadTag" Src="~/Common/HeadTag.ascx" %>
    <%@ Register TagPrefix="telerik" TagName="Footer" Src="~/Common/Footer.ascx" %>
    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <telerik:HeadTag runat="server" ID="Headtag2"></telerik:HeadTag>
        <!-- custom head section -->
        <!-- end of custom head section -->
    </head>
    <body class="BODY">
        <form runat="server" id="mainForm" method="post">
        <telerik:Header runat="server" ID="Header1" NavigationLanguage="VB" ShowSkinChooser="false">
        </telerik:Header>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
        <!-- content start -->
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadGrid ID="RadGrid1" ShowGroupPanel="true" AutoGenerateColumns="false"
            DataSourceID="SqlDataSource1" ShowFooter="True" runat="server" GridLines="None"
            AllowPaging="true" PageSize="7" Skin="Office2007" OnPreRender="RadGrid1_PreRender">
            <MasterTableView ShowGroupFooter="true">
                <GroupHeaderTemplate>
                    <asp:Label runat="server" ID="Label1" Text='<%# "Number of units: " & DirectCast(Container, GridGroupHeaderItem).AggregatesValues("UnitsInStock") %>'
                        Visible='<%# ((DirectCast(Container, GridGroupHeaderItem).AggregatesValues("UnitsInStock")) IsNot Nothing)%>'>
                    </asp:Label>
                    <asp:Label runat="server" ID="Label2" Text='<%#"Number of items in group: " & DirectCast(Container, GridGroupHeaderItem).AggregatesValues("AvailableUnits") %>'
                        Visible='<%# ((DirectCast(Container, GridGroupHeaderItem).AggregatesValues("AvailableUnits")) IsNot Nothing)%>'>
                    </asp:Label>
                    <asp:Label runat="server" ID="Label3" Text='<%# "Product name: "+Eval("ProductName") %>'
                        Visible='<%# ((DirectCast(Container, GridGroupHeaderItem).AggregatesValues("ProductName")) IsNot Nothing)%>'></asp:Label>
                    <asp:Label runat="server" ID="Label4" Text='<%# "Product price: " & Eval("UnitPrice") %>'
                        Visible='<%# ((DirectCast(Container, GridGroupHeaderItem).AggregatesValues("UnitPrice")) IsNot Nothing)%>'></asp:Label>
                    <asp:Label runat="server" ID="Label5" Text='<%# "Total price: " & Eval("TotalPriceResult") %>'
                        Visible='<%# ((DirectCast(Container, GridGroupHeaderItem).AggregatesValues("TotalPriceResult")) IsNot Nothing)%>'>
                    </asp:Label>
                </GroupHeaderTemplate>
                <GroupFooterTemplate>
                    Product count:
                    <asp:Label ID="Label6" runat="server" Text='<%#Eval("ProductName") %>'>
                    </asp:Label>
                    &nbsp;&nbsp;&nbsp;&nbsp; Total units:
                    <asp:Label ID="Label7" runat="server" Text='<%# Eval("UnitsInStock") %>'>
                    </asp:Label>
                    &nbsp;&nbsp;&nbsp;&nbsp; Total sum:
                    <asp:Label ID="Label8" runat="server" Text='<%# Eval("UnitPrice") %>'>
                    </asp:Label>
                    &nbsp;&nbsp;&nbsp;&nbsp; Total price:
                    <asp:Label ID="Label9" runat="server" Text='<%# Eval("TotalPriceResult") %>'>
                    </asp:Label>
                </GroupFooterTemplate>
                <Columns>
                    <telerik:GridBoundColumn Aggregate="Count" DataField="ProductName" HeaderText="Product name"
                        SortExpression="ProductName" UniqueName="ProductName">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn Aggregate="Sum" DataField="UnitsInStock" HeaderText="Units In Stock"
                        SortExpression="UnitsInStock" UniqueName="UnitsInStock">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn Aggregate="Sum" DataField="UnitPrice" HeaderText="Unit price"
                        FooterText="Total price: ">
                    </telerik:GridBoundColumn>
                    <telerik:GridCalculatedColumn HeaderText="Total Price" UniqueName="TotalPrice" DataType="System.Double"
                        DataFields="UnitPrice, UnitsInStock" Expression="{0}*{1}" FooterText="Total : "
                        Aggregate="Sum" />
                </Columns>
                <GroupByExpressions>
                    <telerik:GridGroupByExpression>
                        <GroupByFields>
                            <telerik:GridGroupByField FieldName="UnitsInStock" />
                        </GroupByFields>
                        <SelectFields>
                            <telerik:GridGroupByField FieldName="UnitsInStock" Aggregate="Count" FieldAlias="AvailableUnits"
                                HeaderText="UnitsInStock" />
                        </SelectFields>
                    </telerik:GridGroupByExpression>
                </GroupByExpressions>
            </MasterTableView>
            <ClientSettings AllowDragToGroup="true" />
            <GroupingSettings ShowUnGroupButton="true" />
        </telerik:RadGrid>
        <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT * FROM Products" runat="server"></asp:SqlDataSource>
        <!-- content end -->
        <telerik:Footer runat="server" ID="Footer1"></telerik:Footer>
        </form>
    </body>
    </html>

Get more than expected!

Take your time to truly experience the power of RadControls for ASP.NET AJAX with a free 60-day trial backed up by Telerik’s unlimited dedicated support.

Download your RadControls for ASP.NET AJAX trial and jumpstart your development with the available Getting Started resources.

If you have any questions, do not hesitate to contact us at sales@telerik.com.

Copyright 2002-2012 © Telerik. All right reserved
Telerik Inc, 201 Jones Rd, Waltham, MA 02451