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

Issue with adding aggregate function at runtime.

10 Answers 182 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jan
Top achievements
Rank 1
Jan asked on 02 Mar 2012, 02:53 PM

I am getting this error when adding aggregate culumn at run time;

No generic method 'Sum' on type 'System.Linq.Enumerable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.

private void SearchGrid_AutoGeneratingColumn(object sender,
                                             Telerik.Windows.Controls.GridViewAutoGeneratingColumnEventArgs e)
{
    if (string.IsNullOrEmpty(ReportWizardViewModel.ReportDefinition.Summary)) return;
 
    var fields = ReportWizardViewModel.ReportDefinition.Summary.Split(new char[] { ',' });
 
 
    var dataColumn = e.Column as GridViewDataColumn;
    if (dataColumn != null)
    {
        var columnName = dataColumn.Header.ToString();
        var fieldDef = fields.FirstOrDefault(w => w.StartsWith(columnName));
        if (fieldDef != null)
        {
            var pref = fieldDef.Replace(columnName + "-", "");
            dataColumn.DataMemberBinding.StringFormat = "{0:" + pref + "}";
 
 
            dataColumn.AggregateFunctions.Add(new SumFunction()
                        {
                            Caption = columnName + " :",
                            SourceField = columnName,
                            ResultFormatString = "{}{0:c2}",
                            SourceFieldType = typeof(decimal)
 
                        });
        }
 
 
    }
}

10 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 02 Mar 2012, 02:58 PM
Hello,

 Can you check if you have numeric property with such name in your objects bound to grid?

All the best,
Vlad
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Jan
Top achievements
Rank 1
answered on 02 Mar 2012, 03:07 PM
Yes, I do
I am not sure, I am using a dynamic solution one of you guys posted. Usinng Wcf service generate DataSet to MyDataRow
here is code;

//client
 var client = new ReportServiceClient();
                    client.GetDataCompleted += (s, e) =>
                    {
                        if (e.Error != null)
                        {
                            var error = e.Error.Message;
                            RadWindow.Alert(error);
                            return;
                        }
 
 
                        Data = new ObservableCollection<MyDataRow>(from i in e.Result
                                                                    select
                                                                        new MyDataRow(i));
 
                        RaisePropertyChanged("Data");
                    };
                    client.GetDataAsync(ReportDefinition.Sql);

where MyDataRow
public class MyDataRow : DynamicObject, INotifyPropertyChanged
{
    IDictionary<string, object> data;
 
    public MyDataRow()
    {
        data = new Dictionary<string, object>();
    }
 
    public MyDataRow(IDictionary<string, object> source)
    {
        data = source;
    }
 
    public override IEnumerable<string> GetDynamicMemberNames()
    {
        return data.Keys;
    }
 
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = this[binder.Name];
 
        return true;
    }
 
    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        this[binder.Name] = value;
 
        return true;
    }
 
    public object this[string columnName]
    {
        get
        {
            if (data.ContainsKey(columnName))
            {
                return data[columnName];
            }
 
            return null;
        }
        set
        {
            if (!data.ContainsKey(columnName))
            {
                data.Add(columnName, value);
 
                OnPropertyChanged(columnName);
            }
            else
            {
                if (data[columnName] != value)
                {
                    data[columnName] = value;
 
                    OnPropertyChanged(columnName);
                }
            }
        }
    }
 
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
 
    #region INotifyPropertyChanged Members
 
    public event PropertyChangedEventHandler PropertyChanged;
 
    #endregion
}

0
Vlad
Telerik team
answered on 02 Mar 2012, 03:09 PM
Hello,

 Can you post the property declaration? 

Kind regards,
Vlad
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Jan
Top achievements
Rank 1
answered on 02 Mar 2012, 03:16 PM
0
Vlad
Telerik team
answered on 02 Mar 2012, 03:35 PM
Hello,

 I'm afraid that standard Sum will not work for dynamics. You will need to create custom aggregate function - please check our demos for more info about various custom aggregates.  

Regards,
Vlad
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Jan
Top achievements
Rank 1
answered on 02 Mar 2012, 04:47 PM

Thanks,
Getting this now, Do not know how to fix this
No generic method 'MySum' on type 'HiscoBudget.Controls.CustomSumCalc' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.

client code changes
 
 dataColumn.AggregateFunctions.Add(new CustomSum()
                                {
                                    Caption = columnName + " :",
                                    SourceField = columnName,
                                    ResultFormatString = "{}{0:c2}"
 
                                });



public class CustomSum : EnumerableSelectorAggregateFunction
 {
     protected override string AggregateMethodName
     {
         get { return "MySum"; }
     }
 
     protected override Type ExtensionMethodsType
     {
         get { return typeof(CustomSumCalc); }
     }
 
 }
 
 public static class CustomSumCalc
 {
     public static double MySum<TSource>(IEnumerable<TSource> source, Func<TSource, double> selector)
 
     {
         int itemCount = source.Count();
         if (itemCount > 1)
         {
             IEnumerable<double> values = from i in source select Convert.ToDouble(selector(i));
             return values.Sum();
         }
         return 0;
     }
 }





0
Manoj
Top achievements
Rank 1
answered on 04 Mar 2012, 03:47 PM
I am also struggling to find a solution for same. It is very critical and need to have solution by today. Could you please post the solution ASAP?
0
Jan
Top achievements
Rank 1
answered on 09 Mar 2012, 07:18 PM
Well, I am still not able to find a solution. Vlad's article is a good one but need a better opion to generate or reflect data types so filtering and aggregate function can work
0
Thomas
Top achievements
Rank 1
answered on 14 Mar 2012, 08:05 PM
Can someone help with this one,
No generic method 'MySum' on type 'HiscoBudget.Controls.CustomSumCalc' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.

0
Dimitrina
Telerik team
answered on 15 Mar 2012, 09:01 AM
Hello,

 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
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
GridView
Asked by
Jan
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Jan
Top achievements
Rank 1
Manoj
Top achievements
Rank 1
Thomas
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or