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

[Solved] Display different aggregates between grouping totals and grand totals

3 Answers 128 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stephen Austin
Top achievements
Rank 1
Stephen Austin asked on 24 Mar 2009, 01:47 PM
Hello,
My client would like to display the Max aggregate on sub-totals, and a total aggregate on the grand total. Is there a way to do this using RadGrid?
<telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" Skin="Office2007" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource" ShowFooter="True" ShowStatusBar="True">  
                    <HeaderContextMenu EnableTheming="True">  
                        <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
                    </HeaderContextMenu> 
                    <ExportSettings ExportOnlyData="True" IgnorePaging="True" OpenInNewWindow="True">  
                        <Pdf FontType="Embed" PageBottomMargin="0.25in" PageFooterMargin="0.25in" PageHeaderMargin="0.25in" PageHeight="8.5in" PageLeftMargin="0.25in" PageRightMargin="0.25in" PageTopMargin="0.25in" PageWidth="11in" Title="JPS Report" /> 
                    </ExportSettings> 
                    <FooterStyle Font-Bold="True" /> 
                    <MasterTableView ShowGroupFooter="true">  
                        <GroupByExpressions> 
                            <telerik:GridGroupByExpression> 
                                <GroupByFields> 
                                    <telerik:GridGroupByField FieldName="weeknumber" SortOrder="Ascending" /> 
                                </GroupByFields> 
                                <SelectFields> 
                                    <telerik:GridGroupByField FieldName="weeknumber" HeaderText=":" SortOrder="Ascending" /> 
                                </SelectFields> 
                            </telerik:GridGroupByExpression> 
                        </GroupByExpressions> 
                        <RowIndicatorColumn> 
                            <HeaderStyle Width="20px"></HeaderStyle> 
                        </RowIndicatorColumn> 
                        <ExpandCollapseColumn> 
                            <HeaderStyle Width="20px"></HeaderStyle> 
                        </ExpandCollapseColumn> 
                        <Columns> 
                            <telerik:GridBoundColumn DataField="sup_htstartdate" DataFormatString="{0:d}" HeaderText="HT Start Date" ItemStyle-CssClass="dateColumn" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                                <ItemStyle CssClass="dateColumn"></ItemStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="sup_htenddate" DataFormatString="{0:d}" HeaderText="HT End Date" ItemStyle-CssClass="dateColumn" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                                <ItemStyle CssClass="dateColumn"></ItemStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="scn" HeaderText="Doc Number" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="sup_bidtypename" HeaderText="Bid Type" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="sup_probability" DataFormatString="{0}%" HeaderText="Probability" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="sup_accountidname" HeaderText="Customer" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="sup_plantname" HeaderText="Plant Name" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="sup_city" HeaderText="City" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="sup_state" HeaderText="State" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false" FooterText="Monthly Total:">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="sup_sitespecifictraining" HeaderText="Training" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false" FooterText=" " Aggregate="Max">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="sup_union" HeaderText="Union" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false" FooterText=" " Aggregate="Sum">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="sup_projectmanager" HeaderText="Project Mgr" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false" FooterText=" " Aggregate="Max">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="sup_unitsupervisors" HeaderText="Unit Sup" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false" FooterText=" " Aggregate="Max">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="sup_technician" HeaderText="Techs" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false" FooterText=" " Aggregate="Max">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="sup_administration" HeaderText="Admin" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false" FooterText=" " Aggregate="Max">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="sup_6wayunits" HeaderText="6 Ways" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false" FooterText=" " Aggregate="Max">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="sup_gastrains" HeaderText="Gas Trains" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false" FooterText=" " Aggregate="Max">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="sup_superpower12" HeaderText="SPR 12" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false" FooterText=" " Aggregate="Max">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="sup_weeklyrevenue" HeaderText="Estimate" DataFormatString="{0:c}" ItemStyle-CssClass="moneyColumn" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="false" FooterText="" Aggregate="Max">  
                                <HeaderStyle Font-Bold="False" Font-Size="XX-Small"></HeaderStyle> 
                                <ItemStyle CssClass="moneyColumn"></ItemStyle> 
                            </telerik:GridBoundColumn> 
                        </Columns> 
                    </MasterTableView> 
                    <GroupHeaderItemStyle Font-Bold="True" /> 
                    <ClientSettings AllowKeyboardNavigation="True">  
                    </ClientSettings> 
                    <FilterMenu EnableTheming="True">  
                        <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
                    </FilterMenu> 
                </telerik:RadGrid> 

Thanks!

3 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 30 Mar 2009, 10:12 AM
Hello Stephen,

To attain the functionality you are searching for you will need to calculate the total of the columns' aggregates programmatically. To accomplish this task the code should wire the ItemDataBound event. In its handler you should calculate the values placed in the FooterItem in order to find the grand total/ Max of the sub-totals. Please review this online help article devoted on this matter.

Please give it a try and let me know if I can help you further.

Best Regards, Georgi Krustev
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Stephen Austin
Top achievements
Rank 1
answered on 30 Mar 2009, 01:57 PM
Thanks for the reply, I've been using that example. The issue I have been running into surrounds the use of the group footers. I'm not sure how to go about setting the value of the group footers during my custom calculation.

Thanks!

 

0
Georgi Krustev
Telerik team
answered on 01 Apr 2009, 10:13 AM
Hi Stephen Austin,

If you need to change the value of the group footers you should use this approach:
 int total; 
private void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
 if(e.Item is GridDataItem) 
 { 
    GridDataItem dataItem = e.Item as GridDataItem; 
    int fieldValue = int.Parse(dataItem["Quantity"].Text); 
    total+= fieldValue; 
 } 
 if(e.Item is GridFooterItem) 
 { 
    GridFooterItem footerItem = e.Item as GridFooterItem; 
    footerItem["Quantity"].Text = "total: " + total.ToString(); 
 } 
}  

If you need to add totals of the sub-totals you should use the following approach:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
   { 
        if (e.Item is GridFooterItem) 
       { 
           GridFooterItem footerItem = e.Item as GridFooterItem; 
 
           footerItem[ "ProductName"].Controls.Add(new LiteralControl("Total: <br/>")); 
           footerItem[ "ProductName"].Controls.Add(new LiteralControl("Highest price/units quantity: ")); 
 
           footerItem[ "UnitPrice"].Controls.Add(new LiteralControl("$" + totalPrice.ToString()+ "<br/>")); 
           footerItem[ "UnitPrice"].Controls.Add(new LiteralControl("$" + highestPrice.ToString())); 
 
           footerItem[ "UnitsInStock"].Controls.Add(new LiteralControl(totalUnits.ToString() + "<br/>")); 
           footerItem[ "UnitsInS tock"].Controls.Add(new LiteralControl(highestUnitQuantity.ToString())); 
       } 
        else if (e.Item is GridDataItem) 
       { 
           GridDataItem dataItem = e.Item as GridDataItem; 
 
            string formatPrice = dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex][ "UnitPrice"].ToString(); 
            double fieldValue = Double.Parse(formatPrice); 
           totalPrice += fieldValue; 
 
            if (highestPrice == 0 || highestPrice < fieldValue) 
           { 
               highestPrice = fieldValue; 
           } 
 
           fieldValue = Double.Parse(dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex][ "UnitsInStock"].ToString()); 
           totalUnits += fieldValue; 
 
            if (highestUnitQuantity == 0 || highestUnitQuantity < fieldValue) 
           { 
               highestUnitQuantity = fieldValue; 
           } 
       } 
   } 

For more information please refer to the online help article which I point in my previous post.

Regards,
Georgi Krustev
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Stephen Austin
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Stephen Austin
Top achievements
Rank 1
Share this question
or