Footer sum of one or more fields

6 posts, 0 answers
  1. Brian Graves
    Brian Graves avatar
    48 posts
    Member since:
    May 2010

    Posted 19 May 2010 Link to this post

    I am using the SumFunction for my Column AggregateFunctions.  However, when I use the CountFunction it simply returns the number of rows in my grid.  I was hoping that it would return the sum of all UNIQUE values in my Column.  Is there a way to do a DISTINCT count using the CountFunction?  If not, how can I programmatically bind a function to my column that will return the number of unique values?
  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 21 May 2010 Link to this post

    Hello Brian Graves,

    The attached project demonstrates how to create a custom aggregate function and use it in RadGridView.
    For your needs you will need to modify  MyAggregateMethod. Within the method you have access to the items in RadGridView and may write a few lines of code to return the distinct total.
    Let me know in case you need further assistance.

    Sincerely yours,
    Pavel Pavlov
    the Telerik team

    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 Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft banner
  4. Brian Graves
    Brian Graves avatar
    48 posts
    Member since:
    May 2010

    Posted 21 May 2010 Link to this post

    Thanks...this worked. However, I found that I needed to create a different MyAggregationMethod for every single field in my grid (that I need to do a distinct count on).  Is there any way I can pass a string from the MyAggregateFunction class to the MyAggregateMethod so I know which column that I want to do a distinct sum on?
  5. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 25 May 2010 Link to this post

    Hello Brian Graves,

    I am afraid that it is not possible to pass parameter to the method this way . However I think we can find an alternative way - we can make a custom EnumerableSelectorAggregateFunction instead. This one has the SourceFiled property.

    I will prepare a small sample , but I will need to know the data type of the property you will make distinct count on.

    All the best,
    Pavel Pavlov
    the Telerik team

    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 Public Issue Tracking system and vote to affect the priority of the items.
  6. Brian Graves
    Brian Graves avatar
    48 posts
    Member since:
    May 2010

    Posted 25 May 2010 Link to this post

    There isn't a single data type.  I could be doing a distinct count on a string column, a date column, dollar amount, or even an integer column...it depends what I want to do a distinct count on.  Anyway, I figured out how to do this using the EnumerableSelectorAggregateFunction you've suggested:

     

        public class CountDistinctFunction : EnumerableSelectorAggregateFunction
        {
            protected override string AggregateMethodName
            {
                get
                {
                    return "CountDistinct";
                }
            }

            protected override Type ExtensionMethodsType
            {
                get
                {
                    return typeof(CountDistinctAggregates);
                }
            }
        }

        public static class CountDistinctAggregates
        {
            public static int CountDistinct<TSource>(IEnumerable<TSource> source, Func<TSource, float> selector)
            {
                if (!source.Any<TSource>())
                    return 0;
                else
                    return source.Select(selector).Distinct().Count();
            }

            public static int CountDistinct<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, TResult> selector)
            {
                if (!source.Any<TSource>())
                    return 0;
                else
                {
                    IEnumerable<string> values = from i in source select Convert.ToString(selector(i)).ToLower();
                    var result = values.Distinct().Count();
                    return result;
                }
            }

            public static int CountDistinct<TSource>(IEnumerable<TSource> source, Func<TSource, int> selector)
            {
                if (!source.Any<TSource>())
                    return 0;
                else
                    return source.Select(selector).Distinct().Count();
            }
        }

     

  7. Brian Graves
    Brian Graves avatar
    48 posts
    Member since:
    May 2010

    Posted 26 May 2010 Link to this post

    Oh...forgot to mention.  Here's how you add a CountDistinctFunction to your column:

    col.AggregateFunctions.Add(
    new CountDistinctFunction { SourceField = columnName });

     

Back to Top
DevCraft banner