Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8078053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:40:08+00:00 2026-06-05T15:40:08+00:00

I have a Telerik Grid which has a footer that needs to display column

  • 0

I have a Telerik Grid which has a footer that needs to display column sums. However, one of the colums’ data types is TimeSpan, which isn’t supported by Telerik’s Sum aggregate. I need to use GridBoundColumnBuilder.Aggregate() to add the aggregates. So I guess basically the question is how to reference my custom aggregate in telerik’s Aggregate() method. And if you notice anything else I’m doing wrong, feel free to point it out :)Using this article, I created a class for my custom aggregate, called SumAggregate, shown below. (note that this isn’t finished- its taken from the article. It actually implements a totally different aggregate)

SumAggregate.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Web.Mvc;

namespace TelerikPOC.CustomAggregates
{
    public class SumAggregate : AggregateFunction
    {
        private System.Collections.Generic.List<object> distinctValues;

        /// <summary>
        /// Initializes the current aggregate function to its initial
        /// state ready to accumulate and merge values.
        /// </summary>
        /// <remarks>
        /// This method is called every time the accumulation of values 
        /// must start over for a new subset of records from the data source.
        /// </remarks>
        public void Init()
        {
            this.distinctValues = new System.Collections.Generic.List<object>();
        }

        /// <summary>
        /// Accumulates new argument values to the current aggregate function.
        /// </summary>
        /// <remarks>
        /// This aggregate function accepts one argument:
        /// number - a numeric value to accumulate to the aggregate function;
        /// </remarks>
        public void Accumulate(object[] values)
        {
            if (!distinctValues.Contains(values[0]))
            {
                distinctValues.Add(values[0]);
            }
        }

        /// <summary>
        /// Merges the specified aggregate function to the current one.
        /// </summary>
        /// <param name="Aggregate">
        /// Specifies an aggregate function to be merged to the current one.
        /// </param>
        /// <remarks>
        /// This method allows the reporting engine to merge two accumulated
        /// subsets of the same aggregate function into a single result.
        /// </remarks>
        public void Merge(AggregateFunction aggregate)
        {
            // Accumulate the values of the specified aggregate function.
            System.Collections.Generic.List<object> sums1 =     ((SumAggregate)aggregate).distinctValues;
            foreach (object o in sums1)
            {
                this.Accumulate(new object[] { o });
            }
        }

        /// <summary>
        /// Returns the currently accumulated value of the aggregate function.
        /// </summary>
        /// <returns>
        /// The currently accumulated numeric value of the aggregate function.
        /// </returns>
        public object GetValue()
        {
            return this.distinctValues.Count;
        }
    }
}

And here is the code for adding the aggregates. This conflicts with the code below from index.cshtml, but I wanted to include both methods of adding aggregates just to give more options on the answer. This needs to be modified to use a custom aggregate rather than a built-in one, like it is using now.

GridHelper.cs (unfinished — will add logic to loop through the columns and such later. By the way, if anyone would like to help me out with that too, I’d be very grateful, though I’ll admit I haven’t tried anything yet.)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Web.Mvc.UI.Fluent;
using TelerikPOC.CustomAggregates;

namespace TelerikPOC.Helpers
{
    public class GridHelper
    {
        public static void AddAggregateToColumn(GridBoundColumnBuilder<dynamic> columnBuilder, string Aggregate)
        {
            switch (Aggregate)
            {
                case "Sum":
                    {
                        columnBuilder.Aggregate(aggregates => aggregates.Sum())
                            .GroupFooterTemplate(result => "Sum:" + result.Sum)
                            .ClientFooterTemplate("Sum: <#= Sum #>")
                            .FooterTemplate(result => "Total: " + result.Sum);
                    }
                    break;
            }
        }
    }
}

And then I’m using the HtmlHelper class to build/render the telerik grid, like this:

From Index.cshtml: (be sure to read the comments to the right)

@{
    Html.Telerik()
    .Grid(Model)
    .Name("statisticalGrid")
    .Columns(columns =>
    {
        columns.Bound(o => o.PlanID).Aggregate(something);         //This is probably going to be where 
        columns.Bound(o => o.SessionID).Aggregate(something);      //I need the help. Just not sure
        columns.Bound(o => o.TimeSpan).Aggregate(something);       //how to reference the custom
        columns.Bound(o => o.TimeSpanDouble).Aggregate(something); //aggregate here, in
    })                                                             //place of `something`
    .Sortable(sortable => sortable.Enabled(true))
    .Filterable()
    .Pageable(page => page.PageSize(25))
    .Reorderable(reorder => reorder.Columns(true))
    .Groupable(groupable => groupable.Enabled(true))
    .ClientEvents(events => events
        .OnColumnReorder("onReorder"))
    .Render();
}

So I guess basically the question is how to reference my custom aggregate in telerik’s Aggregate() method. And if you notice anything else I’m doing wrong, feel free to point it out 🙂

Edit: Just noticed that I have to implement the method CreateAggregateExpression(Expression, bool) in the SumAggregate class. Not entirely sure how to implement it, though.

Last Edit: I use a custom column builder method to build out the columms, so I’m not sure exactly how to do the formatting here. I was able to format the sum, but not the rest of the column, since outside the context of the telerik call I don’t have access to the item variable. Here is basically what my column building logic is like:

In the telerik code,

.Columns(a => GridHelper.GenerateColumns(a, Model.SelectedReport))

And generate columns looks something like:

public static void GenerateColumns(GridColumnFactory<dynamic> columnFactory, Company.Project.Data.Entity.Report reportStructure)
        {
            foreach (var columnLayout in reportStructure.ReportCols.OrderBy(o => o.ColumnSequence))
            {
                GridBoundColumnBuilder<dynamic> columnBuilder = columnFactory.Bound(columnLayout.ColumnType);
                //do other stuff here (add aggregates, formatting, etc)
            }

So how would I do the formatting in this context?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:40:09+00:00Added an answer on June 5, 2026 at 3:40 pm

    One thing you could do is add another column that represents the TimeSpan in “Days”. Then you would not have to use custom aggregates.

    Model:

    public List<Objects.Temp> GetTemp()
    {
      List<Objects.Temp> ltemp = new List<Objects.Temp>();
      System.Random r = new Random();
      Objects.Temp t = new Objects.Temp();
      t.Name = "One";
      t.start = DateTime.Now;
      t.Value = r.NextDouble();
      t.ts = DateTime.Today.AddDays(25) - t.start;
      t.tsDays = t.ts.Days;
      ltemp.Add(t);
      t = new Objects.Temp();
      t.Name = "Two";
      t.start = DateTime.Now;
      t.Value = r.NextDouble();
      t.ts = DateTime.Today.AddDays(15) - t.start;
      t.tsDays = t.ts.Days;
      ltemp.Add(t);
      t = new Objects.Temp();
      t.Name = "Three";
      t.start = DateTime.Now;
      t.Value = r.NextDouble();
      t.ts = DateTime.Today.AddDays(55) - t.start;
      t.tsDays = t.ts.Days;
      ltemp.Add(t);
    
      return ltemp;
    }
    

    View:

    @(Html.Telerik().Grid(Model)
      .Name("Grid")
      .Columns(columns =>
      {
        columns.Bound(o => o.Name)
                .Aggregate(aggregates => aggregates.Count())
                .FooterTemplate(@<text>Total Count: @item.Count</text>)
                .GroupFooterTemplate(@<text>Count: @item.Count</text>);
    
        columns.Bound(o => o.start)
                .Width(200)
                .Aggregate(aggreages => aggreages.Max())
          //.Format("{0:c}")
                .FooterTemplate(@<text>Max: @item.Max</text>)
                .GroupFooterTemplate(@<text>Max: @item.Max</text>);
    
        columns.Bound(o => o.Value)
                .Width(200)
                .Aggregate(aggregates => aggregates.Average())
                .FooterTemplate(@<text>Average: @item.Average</text>)
                .GroupFooterTemplate(@<text>Average: @item.Average</text>);
    
        columns.Bound(o => o.ts)
                .Width(100)
                .Aggregate(aggregates => aggregates.Count().Min().Max())
                .FooterTemplate(
                @<text>
                    <div>Min: @item.Min</div>
                    <div>Max: @item.Max</div>
                </text>)
                .GroupHeaderTemplate(@<text>@item.Title: @item.Key (Count: @item.Count)</text>);
    
        columns.Bound(o => o.tsDays)
              .Width(100)
              .Aggregate(aggregates => aggregates.Sum())
              .FooterTemplate(
              @<text>
                    <div>Sum: @item.Sum Days</div>
                </text>)
              .GroupHeaderTemplate(@<text>@item.Title: @item.Key (Sum: @item.Sum)</text>);
      })
        .Sortable()
    

    )

    You get something that looks like this:

    enter image description here

    Version 2

    I updated my answer so that there is only one TimeSpan column with sums the TimeSpan and displays it in TimeSpan format. The data in the column is really TimeSpan.TotalMilliseconds, but it displays as TimeSpan by using a template and formatting a TimeSpan using the TimeSpan.FromMilliseconds method. I think this way doing it is easier than creating a custom aggregate class if that is even possible with MVC extensions.

    Model:

    public class Temp
    {
      public string Name { get; set; }
      public DateTime Start { get; set; }
      public double Value { get; set; }
      public TimeSpan ts { get; set; }
      public double tsMilliseconds { get; set; }
    }
    
    
      List<Objects.Temp> ltemp = new List<Objects.Temp>();
      System.Random r = new Random();
      Objects.Temp t = new Objects.Temp();
      t.Name = "One";
      t.Start = DateTime.Now;
      t.Value = r.NextDouble();
      t.ts = DateTime.Today.AddDays(25) - t.Start;
      t.tsMilliseconds = t.ts.TotalMilliseconds;
      ltemp.Add(t);
      t = new Objects.Temp();
      t.Name = "Two";
      t.Start = DateTime.Now;
      t.Value = r.NextDouble();
      t.ts = DateTime.Today.AddDays(15) - t.Start;
      t.tsMilliseconds = t.ts.TotalMilliseconds;
      ltemp.Add(t);
      t = new Objects.Temp();
      t.Name = "Three";
      t.Start = DateTime.Now;
      t.Value = r.NextDouble();
      t.ts = DateTime.Today.AddDays(55) - t.Start;
      t.tsMilliseconds = t.ts.TotalMilliseconds;
      ltemp.Add(t);
    

    View:

    @(Html.Telerik().Grid(Model)
      .Name("Grid")
      .Columns(columns =>
      {
        columns.Bound(o => o.Name)
                .Aggregate(aggregates => aggregates.Count())
                .FooterTemplate(@<text>Total Count: @item.Count</text>)
                .GroupFooterTemplate(@<text>Count: @item.Count</text>);
    
        columns.Bound(o => o.Start)
                .Template(@<text>@item.Start.ToShortDateString()</text>)
                .Aggregate(aggreages => aggreages.Max())
                .FooterTemplate(@<text>Max: @item.Max.Format("{0:d}")</text>)
                .GroupHeaderTemplate(@<text>Max: @item.Max.Format("{0:d}")</text>)
                .GroupFooterTemplate(@<text>Max: @item.Max.Format("{0:d}")</text>);
    
        columns.Bound(o => o.Value)
                .Width(200)
                .Aggregate(aggregates => aggregates.Average())
                .FooterTemplate(@<text>Average: @item.Average</text>)
                .GroupFooterTemplate(@<text>Average: @item.Average</text>);
    
        columns.Bound(o => o.tsMilliseconds)
              .Width(100)
              .Aggregate(aggregates => aggregates.Sum())
              .Template(@<text>@TimeSpan.FromMilliseconds(@item.tsMilliseconds)</text>)
              .Title("TimeSpan")
              .FooterTemplate(
              @<text>
                    <div>Sum: @TimeSpan.FromMilliseconds( @Convert.ToDouble( @item.Sum.Value.ToString() ) ) </div>
                </text>)
              //header if you group by TimeSpan
              .GroupHeaderTemplate(@<text>@item.Title: @item.Key (Sum: @TimeSpan.FromMilliseconds(@Convert.ToDouble(@item.Sum.Value.ToString())))</text>)
              //footer for grouping
              .GroupFooterTemplate(@<text>Sum: @TimeSpan.FromMilliseconds(@Convert.ToDouble(@item.Sum.Value.ToString()))</text>);
      })
        .Sortable()
        .Groupable(settings => settings.Groups(groups => groups.Add(o => o.Start)))
    ) 
    

    Without grouping produces this:

    TimeSpan Aggregate

    With grouping produces this:

    With Grouping

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a grid with a column which contains a nullable data Html.Telerik().Grid(Model) .Columns(columns
I have a telerik grid in which i have a column where i show
I have a partial view that uses a Telerik MVC Grid, and it has
I am planning to have a (Telerik MVC) grid in which each row has
I have a telerik grid with a dynamic data source (the grid may use
I have been successful with creating a Telerik grid to display a list of
I have an MVC telerik grid that I have set up to do custom
I have a problem to render column template with Telerik Grid for MVC. I'm
I have in my grid a GridTemplateColumn which display INDEF or MA. When it's
I have a form with two different UserControls - one that contains a Telerik

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.