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

Footer sum of one or more fields

5 Answers 273 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Brian Graves
Top achievements
Rank 1
Brian Graves asked on 20 May 2010, 01:41 AM
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?

5 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 21 May 2010, 08:52 AM
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.
0
Brian Graves
Top achievements
Rank 1
answered on 22 May 2010, 01:03 AM
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?
0
Pavel Pavlov
Telerik team
answered on 25 May 2010, 01:12 PM
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.
0
Brian Graves
Top achievements
Rank 1
answered on 25 May 2010, 04:22 PM
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();
        }
    }

 

0
Brian Graves
Top achievements
Rank 1
answered on 26 May 2010, 04:13 PM
Oh...forgot to mention.  Here's how you add a CountDistinctFunction to your column:

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

 

Tags
GridView
Asked by
Brian Graves
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Brian Graves
Top achievements
Rank 1
Share this question
or