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

how to custom the aggregate function

3 Answers 268 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ocean Ju
Top achievements
Rank 1
Ocean Ju asked on 14 Dec 2011, 04:02 AM

I read the example : http://demos.telerik.com/silverlight/#GridView/CustomAggregates and defined a class like this:

using System;
using Telerik.Windows.Data;
using System.Collections.Generic;
using System.Linq;

namespace MSHF.Paris.UIPlatform.Common
{
    public class ObjectSumFunction : EnumerableSelectorAggregateFunction
    {
        protected override string AggregateMethodName
        {
            get
            {
                return "Sum";
            }
        }

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

    public static class ObjectSum
    {
        public static object Sum<TSource>(IEnumerable<TSource> source, Func<TSource, object> selector)
        {
            int itemCount = source.Count();
            if (itemCount > 0)
            {
                IEnumerable<decimal> values = from i in source select Convert.ToDecimal(selector(i));

                return values.Sum();
            }

            return 0;
        }
    }
}

add the function in code:
dataColumn.AggregateFunctions.Add(new ObjectSumFunction()
                        {
                            Caption = "total:",
                            SourceField = "A4",
                            SourceFieldType = typeof(object),
                            ResultFormatString = gd.GridColumnDefinitions[i].DataFormat
                        });

the column type is object(in fact, it is a decimal, but converted to object)

Get the error:No generic method 'Sum' on type 'MSHF.Paris.UIPlatform.Common.ObjectSum' is compatible with the", because it is a Just-In-Time Debugger dialog, the message is trucated.
please see the attached error file.

3 Answers, 1 is accepted

Sort by
0
Ocean Ju
Top achievements
Rank 1
answered on 19 Dec 2011, 07:29 AM
any suggestion?
0
Igor
Top achievements
Rank 1
answered on 20 Apr 2012, 11:15 AM
Do you have tried this: 
Func<TSource, decimal> instead of  Func<TSource, Object> ?
I have the same problem with enum and string types. I don't know which types are available to the selector, but it seam that  string, Object and Enum aren't supported. This is a limitation in my project ,because my grid row data doesn't have numeric fields, only enums and strings. I have to add a dummy integer field only to make the function callable.
In my application I want to report how many rows are orangutan or chimpanzee counting the field ItemType grouped by location.
I use this code:
public static string CountPrimate<T>(IEnumerable<T> source
    , Func<T, int> selector) where T : PrimateViewModel
{
    int  oranCount = source.Count(x =>
        x.ItemType == PrimateTypeEnum.ORAN);
 
    int chimCount  = source.Count(x =>
        x.ItemType == PrimateTypeEnum.CHIM);
 
    return string.Format("Total Chimpanzee: {0}  Orangutan: {1}", chimCount, oranCount);
}

<telerik:GridViewDataColumn DataMemberBinding="{Binding ItemType}">
    <telerik:GridViewDataColumn.AggregateFunctions>
        <local:CountPrimateFunction Caption="" SourceField="DummyInt" />
    </telerik:GridViewDataColumn.AggregateFunctions>
</telerik:GridViewDataColumn>
...
<telerik:RadGridView.GroupDescriptors>
    <telerik:GroupDescriptor Member="Location.Name"  >
    </telerik:GroupDescriptor>
</telerik:RadGridView.GroupDescriptors>

The selector is not used because I don't have any integer fields (only the dummy one)  and using where T : PrimateViewModel 
 I can access to all source fields.  Returning a string I don't need the caption.
Why is the selector limited to numeric source fields? Is there a better way to solve this aggregation problem?
0
Dimitrina
Telerik team
answered on 21 Apr 2012, 07:31 AM
Hi,

 If the custom aggregates do not work in your case, then I would recommend you to try the approach suggested in this forum thread.

Regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Ocean Ju
Top achievements
Rank 1
Answers by
Ocean Ju
Top achievements
Rank 1
Igor
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or