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

Radgrid footer to show sum of all values

2 Answers 862 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lokesh
Top achievements
Rank 1
Lokesh asked on 07 Jun 2013, 06:38 AM
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 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 07 Jun 2013, 07:35 AM
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
0
Lokesh
Top achievements
Rank 1
answered on 07 Jun 2013, 12:16 PM
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);
            }
        }
Tags
Grid
Asked by
Lokesh
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Lokesh
Top achievements
Rank 1
Share this question
or