Radgrid footer to show sum of all values

3 posts, 1 answers
  1. Lokesh
    Lokesh avatar
    89 posts
    Member since:
    Jul 2012

    Posted 07 Jun 2013 Link to this post

    Hi Team,
    I have a radgrid with a template column that has numeric values bound to it.
    I want to show the sum of all values in a column footer. I tried using Aggregate="Sum" but it gives the sum of values on a single page.
    My grid has almost 200 pages and I want to show the sum of ALL values in that column in column footer.

    Here is my grid:

    <telerik:RadGrid ID="rgGLLineItem" runat="server" OnNeedDataSource="rgGLLineItem_NeedDataSource" OnItemDataBound="rgGLLineItem_ItemDataBound" AllowFilteringByColumn="true" CssClass="GridHieght" ShowFooter="true">
                            <MasterTableView>
                                <Columns>
    <telerik:GridTemplateColumn DataField="AmountInLocalCurr" HeaderText="Amount In LocalCurr." UniqueName="AmountInLocalCurrency" CurrentFilterFunction="Contains" AutoPostBackOnFilter="true" >
                                        <ItemTemplate>
                                           <asp:Label ID="lblAmountInLocalCurr" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.AmountInLocalCurr","{0:N}") %>' />
                                           <asp:Label  ID="lblCrDr" runat="server" />
                                        </ItemTemplate>
                                        <FooterStyle HorizontalAlign="Right"/>
                                        <ItemStyle HorizontalAlign="Right" />
                                    </telerik:GridTemplateColumn>

    </Columns>
                            </MasterTableView>
                          <GroupingSettings CaseSensitive="False" />
                          <ClientSettings Scrolling-AllowScroll="true" Scrolling-UseStaticHeaders="true" ClientEvents-OnMasterTableViewCreating="SetGridHeight"></ClientSettings>
                        </telerik:RadGrid>


    Another problems is with the Filters. As this column is Template column, with no specific data type specified, I think it should accept string input as well. Because,  in ItemDataBound Event, I am setting a text to the label lblCrDr , Cr or Dr depending upon the value of a column.

    This is the DataBound Event:

     if (e.Item is GridDataItem)
                    {
                        GridDataItem item = e.Item as GridDataItem;

                        Label lblCrDr = (Label)item.FindControl("lblCrDr");

                        if (item["AccountJournalEventCRDR"].Text == "1")
                        {
                            lblCrDr.ForeColor = System.Drawing.Color.Red;
                            lblCrDr.Text = "(Cr) ";
                        }
                        if (item["AccountJournalEventCRDR"].Text == "2")
                        {
                            lblCrDr.ForeColor = System.Drawing.Color.Gray;
                            lblCrDr.Text = "(Dr) ";
                        }
                    }

    Could you please help me on this?
    Any help appreciated.

    Thanks,
    Lok..
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 07 Jun 2013 Link to this post

    Hi,

    Please check the below code,its an example , which i have tried, it works fine on my side.

    ASPX:
    <telerik:RadGrid ID="rgGLLineItem" runat="server" DataSourceID="SqlDataSource1" AllowPaging="true"
        AllowFilteringByColumn="true" ShowFooter="true" Width="500px" OnItemDataBound="rgGLLineItem_ItemDataBound">
        <MasterTableView AutoGenerateColumns="false">
            <Columns>
                <telerik:GridTemplateColumn AllowFiltering="false">
                    <ItemTemplate>
                        <asp:Label ID="lblCrDr" runat="server" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridBoundColumn HeaderText="FirstName" DataField="FirstName" UniqueName="FirstName">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn DataField="EmployeeID" HeaderText="EmployeeID" Aggregate="Sum"
                    AllowFiltering="true" UniqueName="EmployeeID" AutoPostBackOnFilter="true">
                    <ItemTemplate>
                        <asp:Label ID="lblAmountInLocalCurr" runat="server" Text='<%# Eval("EmployeeID") %>' />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
        <GroupingSettings CaseSensitive="False" />
        <ClientSettings Scrolling-AllowScroll="true" Scrolling-UseStaticHeaders="true">
        </ClientSettings>
    </telerik:RadGrid>

    C#:
    protected void rgGLLineItem_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
     
            Label lblCrDr = (Label)item.FindControl("lblCrDr");
     
            if (item["FirstName"].Text == "Jeff")
            {
                lblCrDr.ForeColor = System.Drawing.Color.Red;
                lblCrDr.Text = "(Cr) ";
            }
            if (item["FirstName"].Text == "Sam")
            {
                lblCrDr.ForeColor = System.Drawing.Color.Gray;
                lblCrDr.Text = "(Dr) ";
            }
        }
    }

    Thanks
    Princy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Lokesh
    Lokesh avatar
    89 posts
    Member since:
    Jul 2012

    Posted 07 Jun 2013 Link to this post

    Hi Princy.
    Thank you very much for your help.
    Your solution works with no doubt. As always :) 

    But sorry, I forgot to mention that I want to do some calculations on the value and then show the sum of those values in footer.
    I tried doing it in ItemDataBound but it gave me the sum of values for the particular page only.

    So I moved the code to DataBound event and now it is working fine.

    This is what I have done:

    protected void rgGLLineItem_DataBound(object sender, EventArgs e)
            {
                try
                {
                    DataSet ds = new DataSet();
                    ds = GetGLAccountLineItem();

                    foreach (DataRow  row in ds.Tables[0].Rows)
                    {
                        if (row["AccountJournalEventCRDR"].ToString() == "1")
                            Total += Convert.ToDouble(row["AmountInLocalCurr"].ToString()) * -1;
                        if (row["AccountJournalEventCRDR"].ToString() == "2")
                            Total += Convert.ToDouble(row["AmountInLocalCurr"].ToString());
                    }

                    GridFooterItem footerItem = (GridFooterItem)rgGLLineItem.MasterTableView.GetItems(GridItemType.Footer)[0];
                    footerItem["AmountInLocalCurrency"].Text = "Total : " + Total.ToString("N");
                }
                catch (Exception ex)
                {
                    ProcessException(this, ex);
                }
            }
Back to Top