C# – Telerik RadGrid and Running Totals with Dynamic Columns

asp.netcnetradgridtelerik

I have a RadGrid that has a datasource with dynamic columns. I am also doing grouping on the data and displaying the totals for the columns in the group footer using this code in the columncreated event:

if (e.Column.ColumnType != "GridExpandColumn" & e.Column.ColumnType != "GridGroupSplitterColumn")
    {
        if (e.Column.UniqueName != "Item" && e.Column.UniqueName != "Width" && e.Column.UniqueName != "Type" && e.Column.UniqueName != "Balance")
        {
            ((Telerik.Web.UI.GridBoundColumn)e.Column).Groupable = true;
            ((Telerik.Web.UI.GridBoundColumn)e.Column).DataFormatString = "{0:N0}";
            ((Telerik.Web.UI.GridBoundColumn)e.Column).Aggregate = Telerik.Web.UI.GridAggregateFunction.Sum;
        }
    }

My client would like the grouped footer totals to be a running total. ie if the first total is 100 and the second total is 25 then the total in the footer for the second one should display 75. Is there a way to do this with a custom aggregate or any other way?

Any help would be great. Thx!

Best Answer

I don't think there is any inbuild functionlity/field/column in telerik grid to show running total , however you can achieve this in grid's ItemDataBound event by calculating, storing and binding it to footer template column , following is the sample code:

protected void RadGrid1_ItemDataBound1(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem dataItem = e.Item as GridDataItem;
        string payment = dataItem["PaymentAmount"].Text;
        decimal fieldValue = decimal.Parse(payment);
        total += fieldValue;
        dataItem["Balance"].Text = total.ToString();
    }

    if (e.Item is GridFooterItem)
    {
        GridFooterItem footerItem = e.Item as GridFooterItem;
        footerItem["PaymentAmount"].Text = "total: " + total.ToString();
    }
}

Let me know if I am missing something.

Related Topic