I need your help to customize Grid Group header/ Group Header Template

6 posts, 0 answers
  1. Aminul
    Aminul avatar
    11 posts
    Member since:
    Jan 2014

    Posted 25 Aug 2014 Link to this post

    Hello,

    I am getting issue to customize group header, I have some operation(sum,count,min,max, last, first, average) in footer.
    But I want to do same operation in group header.  Example: I have last product in footer now I want to show total product count in header.
    How can I do this? 

    [I can have max 3 column for grouping]

    Could you please help me anyone?
    Please see attached image.
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 25 Aug 2014 in reply to Aminul Link to this post

    Hi Aminul,

    I guess you are using GroupHeaderTemplate. In order to achieve you requirement you can add a Label and get the sum and count, use a unique column to get count. Please try the following code snippet:

    ASPX:
    Unit Price:
    <asp:Label ID="lblUnitPrice" runat="server" Text='<%# "Sum:"+Eval("UnitPrice")+ "Count: "+((GridGroupHeaderItem)Container).AggregatesValues["AvailableUnits"] %>' />

    Thanks,
    Princy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Aminul
    Aminul avatar
    11 posts
    Member since:
    Jan 2014

    Posted 25 Aug 2014 Link to this post

    Hello Princy,

    Thanks for your code snippet. its working but I have to do this in code behind(C#), because our group fields and operation fields will come dynamically (as per our settings)
    So could you please give me the c# code snippet.

    Thanks
    Aminul
  5. Aminul
    Aminul avatar
    11 posts
    Member since:
    Jan 2014

    Posted 25 Aug 2014 in reply to Aminul Link to this post

    Your code not working, Showing invalid result. please see in attachment.

    My code was:

    <telerik:RadGrid ID="RadGrid1" ShowGroupPanel="true" AutoGenerateColumns="false"
        DataSourceID="SqlDataSource1" ShowFooter="false" runat="server" GridLines="None"
        AllowPaging="true" PageSize="20" Skin="Office2007" OnPreRender="RadGrid1_PreRender"
        GroupingSettings-RetainGroupFootersVisibility="true">
        <MasterTableView ShowGroupFooter="true">
            <GroupHeaderTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# "Total Unit Price:"+Eval("UnitPrice")+ ", Count: "+((GridGroupHeaderItem)Container).AggregatesValues["AvailableUnits"] %>'></asp:Label>
            </GroupHeaderTemplate>
            <GroupFooterTemplate>
                Product count: <asp:Label ID="Label6" runat="server" Text='<%#Eval("ProductName") %>'></asp:Label>
                </td>
                <td>
                    Total Units:<asp:Label ID="Label7" runat="server" Text='<%# Eval("UnitsInStock") %>'></asp:Label>
                </td>
                <td>
                    Total Unit Price:<asp:Label ID="Label8" runat="server" Text='<%# Eval("UnitPrice") %>'></asp:Label>
                </td>
                <td>
                    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="Sum" FieldAlias="AvailableUnits"
                            HeaderText="UnitsInStock" />
                    </SelectFields>
                </telerik:GridGroupByExpression>
            </GroupByExpressions>
        </MasterTableView>
        <ClientSettings AllowDragToGroup="true" />
        <GroupingSettings ShowUnGroupButton="true" />
    </telerik:RadGrid>


  6. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 26 Aug 2014 in reply to Aminul Link to this post

    Hi Aminul,

    Please take a look at the following sample code snippet. I have provided solution for both aspx and c# code.

    ASPX:
    <telerik:RadAjaxManager ID="RadAjaxManagerGrid" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="rgrdSample">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="rgrdSample"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadGrid ID="rgrdSample" ShowGroupPanel="true" AutoGenerateColumns="false" DataSourceID="dsGrid" ShowFooter="True" runat="server" GridLines="None" AllowPaging="true" PageSize="7"  GroupingSettings-RetainGroupFootersVisibility="true" OnItemDataBound="rgrdSample_ItemDataBound"
        <MasterTableView ShowGroupFooter="true">
            <GroupHeaderTemplate>
            <%-- .... From ASPX.... --%>
                <%--   <asp:Label runat="server" ID="lblUnitPrices" Text='<%# "Sum:"+Eval("Price") + "Count: "+((GridGroupHeaderItem)Container).AggregatesValues["AvailableUnits"] %>'></asp:Label>--%>
     
            <%-- ....From Code Behind.... --%>
                <asp:Label ID="lblUnitPrice" runat="server" Text="Label"></asp:Label>
            </GroupHeaderTemplate>
            <GroupFooterTemplate>
                     Total sum:
                <asp:Label ID="lblFooterUnitPrice" runat="server" Text='<%# Eval("UnitPrice") %>'>
                </asp:Label>
            </GroupFooterTemplate>
            <Columns>
                <telerik:GridBoundColumn Aggregate="Count" DataField="ProductName" HeaderText="Product name" SortExpression="ProductName" UniqueName="ProductName">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn Aggregate="Count" 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">
                </telerik:GridCalculatedColumn>
            </Columns>
            <GroupByExpressions>
                <telerik:GridGroupByExpression>
                    <SelectFields>
                        <telerik:GridGroupByField FieldName="UnitPrice" FieldAlias="Price" HeaderText="Price" Aggregate="Sum" />
                        <telerik:GridGroupByField FieldName="UnitsInStock" FieldAlias="AvailableUnits" HeaderText="Count" Aggregate="Count" />
                    </SelectFields>
                    <GroupByFields>
                        <telerik:GridGroupByField FieldName="UnitPrice" SortOrder="Descending" />
                    </GroupByFields>
                </telerik:GridGroupByExpression>
            </GroupByExpressions>
        </MasterTableView>
        <ClientSettings AllowDragToGroup="true">
            <ClientEvents />
        </ClientSettings>
        <GroupingSettings ShowUnGroupButton="true"></GroupingSettings>
    </telerik:RadGrid>
    <asp:SqlDataSource ID="dsGrid" ConnectionString="<%$ ConnectionStrings:Northwind_newConnectionString3 %>"
        SelectCommand="SELECT * FROM Products" runat="server"></asp:SqlDataSource>

    C#:
    protected void rgrdSample_ItemDataBound(object sender, GridItemEventArgs e)
    {
        double total = 0;       
        if (e.Item is GridGroupHeaderItem)
        {
            DataRowView dataItem = (DataRowView)e.Item.DataItem;
            GridGroupHeaderItem headerItem = (GridGroupHeaderItem)e.Item;
            Label lblUnitPrice = (Label)headerItem.FindControl("lblUnitPrice");
            total = Convert.ToDouble(dataItem["Price"]);
            int count = Convert.ToInt16(headerItem.AggregatesValues["AvailableUnits"]);
            lblUnitPrice.Text = "Unit Price(Sum:" + total + ", Count:" + count + ")";
        }       
    }

    Thanks,
    Princy
  7. Youssef
    Youssef avatar
    1 posts
    Member since:
    Mar 2015

    Posted 02 Mar 2015 Link to this post

    Hi Princy

    In the same example . How do I align those columns with the corresponding column in the grid.

    In my case it is a grid with multiple groups.

    Can you help me with this ?
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017