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

How to add total in footer in case of editable grid with paging?

3 Answers 176 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lina
Top achievements
Rank 1
Lina asked on 01 Jun 2014, 06:49 PM
How to add total in footer in case of editable grid with paging and DataSourceID?

Now I'm adding total in ItemDataBound by summarize the values and update GridFooterItem
but in this case because of the paging, it doesn't summarize them all, just
the rows in current page. i need to summarize all and update the total
when the grid content is updated.
what is the correct approach in this case?

aspx:
-----
<telerik:RadWindowManager ID="RadWindowManager1" runat="server">
</telerik:RadWindowManager>

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="RadGrid1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>

<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
</telerik:RadAjaxLoadingPanel>

<telerik:RadSplitter ID="mainSplitter" runat="server" Height="100%" Width="100%" 
Orientation="Horizontal" VisibleDuringInit="false" BorderSize="0" PanesBorderSize="0">
<telerik:RadPane ID="paneTitle" runat="server" Height="50px">
<h1>Production Groups</h1>
</telerik:RadPane>
<telerik:RadPane ID="paneGrid" runat="server">


<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" 
AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" 
GridLines="None" Width="1200px" Height="95%"
AllowAutomaticDeletes="True" 
AllowAutomaticInserts="True" 
AllowAutomaticUpdates="True" 
        OnItemDataBound="RadGrid1_ItemDataBound"
        OnDataBound="RadGrid1_DataBound"
        >
<ClientSettings EnableRowHoverStyle="true">
<Scrolling AllowScroll="True" UseStaticHeaders="True" />
</ClientSettings>

<MasterTableView DataKeyNames="GroupID" DataSourceID="ObjectDataSource1" CommandItemDisplay="Top" EditMode="PopUp" ShowFooter="true">
<CommandItemSettings ExportToPdfText="Export to Pdf" AddNewRecordText="Add New Production Group"></CommandItemSettings>

<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>

<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
<Columns>
<telerik:GridEditCommandColumn HeaderText="Edit" ButtonType="ImageButton">
<ItemStyle HorizontalAlign="Center" Width="50px" />
<HeaderStyle HorizontalAlign="Center" Width="50px" />
</telerik:GridEditCommandColumn>
<telerik:GridButtonColumn ConfirmText="Delete this group?" ConfirmDialogType="RadWindow" 
ConfirmTitle="Delete" ButtonType="ImageButton" HeaderText="Delete"
CommandName="Delete" Text="Delete" UniqueName="DeleteColumn">
<ItemStyle HorizontalAlign="Center" Width="50px" />
<HeaderStyle HorizontalAlign="Center" Width="50px" />
</telerik:GridButtonColumn>
<telerik:GridBoundColumn DataField="GroupID" HeaderText="ID" ReadOnly="true" 
SortExpression="GroupID" UniqueName="GroupID" DataType="System.Int32">
<ItemStyle HorizontalAlign="Center" Width="50px" />
<HeaderStyle HorizontalAlign="Center" Width="50px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="GroupName" HeaderText="Group Name" 
SortExpression="GroupName" UniqueName="GroupName">
<ItemStyle  Width="250px" />
<HeaderStyle  Width="250px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="SortOrder" HeaderText="Sort Order" 
SortExpression="SortOrder" UniqueName="SortOrder">
<ItemStyle HorizontalAlign="Center" Width="80px" />
<HeaderStyle HorizontalAlign="Center" Width="80px" />
</telerik:GridBoundColumn>
            
<telerik:GridBoundColumn DataField="ProductionCost" HeaderText="Production Cost" 
SortExpression="ProductionCost" UniqueName="ProductionCost" DataFormatString="{0:c2}">
<ItemStyle HorizontalAlign="Center" Width="80px" />
<HeaderStyle HorizontalAlign="Center" Width="80px" />
</telerik:GridBoundColumn>

</Columns>
<EditFormSettings CaptionDataField="GroupName"
                          CaptionFormatString="Edit Production Group: {0}"
                          InsertCaption="New Production Group"
                          EditFormType="Template" >
<EditColumn ButtonType="ImageButton">
</EditColumn>
<PopUpSettings Modal="true" />
            <FormTemplate>
                    <table id="Table1" cellspacing="1" cellpadding="1" width="270" border="0">
                        <tr>
                            <td>
                            </td>
                            <td>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Group Name:
                            </td>
                            <td>
                                <telerik:RadTextBox ID="TextBox10" Text='<%# Bind("GroupName") %>' runat="server">
                                </telerik:RadTextBox>
                            </td>
                        </tr>
                        
                        <tr>
                            <td>
                                Sort Order:
                            </td>
                            <td>
                                <telerik:RadTextBox ID="TextBox11" Text='<%# Bind("SortOrder") %>' MaxLength="2" runat="server">
                                </telerik:RadTextBox>
                            </td>
                        </tr>

                        <tr>
                            <td>
                                Production Cost:
                            </td>
                            <td>
                                <telerik:RadNumericTextBox ID="txtProductionCost" runat="server" Width="100px" Type="Currency"  Text='<%# Bind("ProductionCost") %>'>
                                    <NumberFormat DecimalDigits="2" />
                                </telerik:RadNumericTextBox>
                            </td>
                        </tr>
                      

                    </table>
                    <table style="width: 100%">
                        <tr>
                            <td align="left" colspan="2">
                                <asp:Button ID="Button1"
                                    runat="server" class="rgUpdate" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
                                </asp:Button>&nbsp;
                                <asp:Button ID="Button2" class="rgCancel" runat="server" CausesValidation="False" CommandName="Cancel">
                                </asp:Button>
                            </td>
                        </tr>
                    </table>
            </FormTemplate>
</EditFormSettings>
</MasterTableView>
</telerik:RadGrid>
</telerik:RadPane>
</telerik:RadSplitter>

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
DeleteMethod="DeleteProductionGroup" InsertMethod="InsertProductionGroup" 
SelectMethod="GetAllProductionGroups" TypeName="comp.BL.ProductionController" 
UpdateMethod="UpdateProductionGroup">
<DeleteParameters>
<asp:Parameter Name="GroupID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="GroupName" Type="String" />
<asp:Parameter Name="sortOrder" Type="Int32" />
            <asp:Parameter Name="ProductionCost" Type="Decimal" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="GroupID" Type="Int32" />
<asp:Parameter Name="GroupName" Type="String" />
<asp:Parameter Name="sortOrder" Type="Int32" />
            <asp:Parameter Name="ProductionCost" Type="Decimal" />
</UpdateParameters>
</asp:ObjectDataSource>


aspx.cs
-------

double total_ProductionCost = 0;

    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {

        if (e.Item is GridFooterItem)
        {
            GridFooterItem footerItem = e.Item as GridFooterItem;
            footerItem["ProductionCost"].Text = "" + String.Format("{0:C2}", total_ProductionCost);
        }

        if (e.Item.ItemType != GridItemType.Item && e.Item.ItemType != GridItemType.AlternatingItem) return;
        tblProductionGroup item = (tblProductionGroup)e.Item.DataItem;
        if (item.ProductionCost.HasValue)
        {
            double ProductionCost = (double)item.ProductionCost;
            total_ProductionCost += ProductionCost;
        }
        var source = RadGrid1.DataSource;

    }

3 Answers, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 04 Jun 2014, 11:24 AM
Hello Lina,

In order to calculate and display values in the RadGrid footer you will have to set the Aggregate property for the columns where you want to display the values. Following is a very simple example demonstrating how to achieve such result:
<telerik:RadGrid runat="server" DataSourceID="SqlDataSource1" ShowFooter="true" AllowPaging="true">
    <MasterTableView>
        <Columns>
            <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
            <telerik:GridBoundColumn DataField="ProductID"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ProductName"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="UnitPrice" Aggregate="Sum"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="UnitsInStock" Aggregate="Sum"></telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
 
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:NorthwindConnectionString %>'
    SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice], [UnitsInStock] FROM [Products]"></asp:SqlDataSource>

In the above example, the UnitPrice and UnitsInStock columns will display the sum of all items (regardless the current page) in the footer.

Detailed information for the column's aggregates is available at the following help article:
Hope this helps.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Ramey
Top achievements
Rank 1
answered on 24 Feb 2016, 05:59 PM
When using the Column Aggregates, I have a column that is a varchar in my database so if I set the Aggregate to sum it throws me a string error.  Is there any way around this?
0
Eyup
Telerik team
answered on 29 Feb 2016, 01:02 PM
Hi Ramey,

I am afraid not. This is not Telerik specific, but general behavior - the operations like Sum, Average, Max, Min, etc. are designed primarily for numeric calculations. Logically, only the Count function can be performed for different data type. For example, it is not possible to calculate the Sum of strings, i.e. text values as in your case.

I hope the clarification was helpful.


Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Lina
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Ramey
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or