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

Aggregate function sum exception

8 Answers 143 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Oliver
Top achievements
Rank 1
Oliver asked on 19 Jan 2012, 10:58 PM
Hi,

I have a RadGridView, depending of the grid type I need, I build dynamically each columns and each aggregate functions. Each time I build a grid, at least one decimal column exist. For othe aggregate functions than SUM, it work...
 
When I assign the ItemSource to the array of objects I receive from the back-end, I receive the following message: "Aucune méthode 'Sum' sur le type 'System.Linq.Enumerable' n'est compatible avec les arguments fournis."

xaml:

<

 

telerik:RadGridView x:Name="radGridViewList" Margin="5 0 5 5" Visibility="Visible" RowDetailsVisibilityMode="Collapsed" FrozenColumnCount="1"

 

 

RowIndicatorVisibility="Collapsed" IsReadOnly="True" AutoGenerateColumns="False" CanUserFreezeColumns="False" Grid.Row="3"

 

 

CanUserResizeColumns="True" ShowColumnFooters="True" ShowGroupFooters="True" SelectionMode="Extended" IsSynchronizedWithCurrentItem="True"> />

 


Code behind:

  static public void SetColumns(RadGridView pGrid, ColumnDescriptor[] pColumnNames)
  {
   if (pColumnNames != null)
   {               
                EnumerableAggregateFunctionBase aggFunc;

    for (int iColumnIndex = 0; iColumnIndex < pColumnNames.Length; iColumnIndex++)
    {
     ColumnDescriptor oneName = pColumnNames[iColumnIndex];
     GridViewDataColumn oneColumn = new Telerik.Windows.Controls.GridViewDataColumn();

     oneColumn.Header = oneName.ColumnName;
     
                    //Apply proper column display format.
     switch (oneName.Format.ToUpper())
     {
      case "$":
       oneColumn.DataFormatString = "C2";
       break;

      case "D":
       oneColumn.DataFormatString = "yyyy'-'MM'-'dd";                           
       break;

      case "H":
       oneColumn.DataFormatString = "HH':'mm";                           
       break;

      case "DH":
       oneColumn.DataFormatString = "yyyy'-'MM'-'dd' 'HH':'mm";
       break;
     }

     oneColumn.Width = new GridViewLength(1, GridViewLengthUnitType.Star);
     oneColumn.ShowFilterButton = true;
     oneColumn.IsSortable = true;
     oneColumn.IsFilterable = true;
     oneColumn.DataMemberBinding = new Binding(string.Format("ElementCells[{0:D}]", iColumnIndex));
     oneColumn.TextAlignment = (TextAlignment)oneName.TextAlignment;
     oneColumn.HeaderTextAlignment = (TextAlignment)oneName.TextAlignment;

                    //Create proper aggregate functions.
                    if (oneName.Agregation != AgregationFunction.None)
                    {
                        aggFunc = null;

                        switch (oneName.Agregation)
                        {
                            case AgregationFunction.Average:
                                aggFunc = new AverageFunction()
                                {
                                    Caption = LocGeneral.GetControlText("Average") + ": "
                                };
                                break;
                            case AgregationFunction.Count:
                                aggFunc = new CountFunction()
                                {
                                    Caption = LocGeneral.GetControlText("Count") + ": "
                                };
                                break;
                            case AgregationFunction.FirstValue:
                                aggFunc = new FirstFunction()
                                {
                                    Caption = LocGeneral.GetControlText("First") + ": "
                                };
                                break;
                            case AgregationFunction.LastValue:
                                aggFunc = new LastFunction()
                                {
                                    Caption = LocGeneral.GetControlText("Last") + ": "
                                };
                                break;
                            case AgregationFunction.MaxValue:
                                aggFunc = new MaxFunction()
                                {
                                    Caption = LocGeneral.GetControlText("Max") + ": "
                                };
                                break;
                            case AgregationFunction.MinValue:
                                aggFunc = new MinFunction()
                                {
                                    Caption = LocGeneral.GetControlText("Min") + ": "
                                };
                                break;
                            case AgregationFunction.Sum:
                                aggFunc = new SumFunction()
                                {
                                    Caption = LocGeneral.GetControlText("Sum") + ": ",
                                    SourceFieldType = typeof(Decimal)
                                };
                                break;

                        }

                        //If aggregate function created, we add it to the column.
                        if (aggFunc != null)
                        {
                            oneColumn.AggregateFunctions.Add(aggFunc);
                        }
                    }

     pGrid.Columns.Add(oneColumn);
    }
   }
  }


using System;
using System.Diagnostics;
using System.Runtime.Serialization;

namespace com.christiegrp.Neuron.Client
{
 [DataContract]
 public class CompleteConfiguration : NeuronClientBase
 {
  #region Constructors.
  public CompleteConfiguration(bool pReadFromDatabase)
   : base(pReadFromDatabase)
  {
  }
  #endregion

  #region NeuronClientBase overrides.
  protected override string getListBoxDisplay()
  {
   return Name;
  }

  protected override string[] getColumnNames()
  {
   switch (GuiLanguage)
   {
    case Languages.LanguageEn:
     return new string[] { "Code", "Name", "Contents" };

    case Languages.LanguageFr:
     return new string[] { "Code", "Nom", "Contenu" };
   }

   return null;
  }

  protected override object[] getItems()
  {
   return new object[] { Code, Name, Contents };
  }

  protected override void copyFrom(NeuronClientBase pSource)
  {
   base.copyFrom(pSource);

   CompleteConfiguration theSource = pSource as CompleteConfiguration;

   if (theSource == null)
   {
    throw new ArgumentException(WrongArgumentType, "pSource");
   }

   Code = theSource.Code;
   Name = theSource.Name;
   Contents = theSource.Contents;
   IsVisible = theSource.IsVisible;
  }
  #endregion

  #region Properties from main table.
  [DataMember]
  public string Code
  {
   [DebuggerStepThrough]
   get { return mCode; }
   [DebuggerStepThrough]
   set
   {
    if (mCode != value)
    {
     mCode = value;
     NotifyOfPropertyChange("Code");
    }
   }
  }

  [DataMember]
  public string Name
  {
   [DebuggerStepThrough]
   get { return mName; }
   [DebuggerStepThrough]
   set
   {
    if (mName != value)
    {
     mName = value;
     NotifyOfPropertyChange("Name");
    }
   }
  }

  [DataMember]
  public string Contents
  {
   [DebuggerStepThrough]
   get { return mContents; }
   [DebuggerStepThrough]
   set
   {
    if (mContents != value)
    {
     mContents = value;
     NotifyOfPropertyChange("Contents");
    }
   }
  }

  [DataMember]
  public bool IsVisible
  {
   [DebuggerStepThrough]
   get { return mIsVisible; }
   [DebuggerStepThrough]
   set
   {
    if (mIsVisible != value)
    {
     mIsVisible = value;
     NotifyOfPropertyChange("IsVisible");
    }
   }
  }
  #endregion

  #region Private fields.
  private string mCode;
  private string mName;
  private string mContents;
  private bool mIsVisible;
  #endregion
 }

 [DataContract]
 public class ColumnDescriptor
 {
  [DataMember]
  public string ColumnName { get; set; }

  [DataMember]
  public ColumnAlignment TextAlignment { get; set; }

  [DataMember]
  public AgregationFunction Agregation { get; set; }

  [DataMember]
  public int GroupingPosition { get; set; }

  [DataMember]
  public string Format { get; set; }
 }

 [DataContract]
 public class ListElements
 {
  public ListElements()
  {
   ElementGuid = Guid.Empty;
   ElementCells = null;
   IsSelected = false;
  }

  [DataMember]
  public Guid ElementGuid { get; set; }
  [DataMember]
  public object[] ElementCells { get; set; }
  public bool IsSelected { get; set; }
 }

 public enum ListContext
 {
  PatientExams, ToBeBilledExamsPublic, NotTransmittedClaimsPublic, TransmittedClaimsPublic, PaidClaimsPublic, InErrorClaimsPublic, ToBeBilledExamsPrivate, NotTransmittedClaimsPrivate, TransmittedClaimsPrivate,
  PaidClaimsPrivate, InErrorClaimsPrivate, NmbListContexts
 }

 public enum AgregationFunction
 {
  None, Average, MinValue, MaxValue, FirstValue, LastValue, Count, Sum, NmbAgregationFunction
 }

 public enum ColumnAlignment
 {
  Left, Right, Center
 }
}

8 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 20 Jan 2012, 08:30 AM
Hi,

 You need to set SourceField property for functions like Sum and Average. 

All the best,
Vlad
the Telerik team

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

0
Oliver
Top achievements
Rank 1
answered on 20 Jan 2012, 03:27 PM
Hi Vlad,

even if I do the following:

aggFunc =

new SumFunction()

 

{

Caption =

LocGeneral.GetControlText("Sum") + ": ",

 

SourceField =

"ClaimedAmount"

 

};


I still having the same problem :(

Thank's
0
Vlad
Telerik team
answered on 20 Jan 2012, 03:32 PM
Hi,

 Can you post this property declaration? What is the property type?

All the best,
Vlad
the Telerik team

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

0
Oliver
Top achievements
Rank 1
answered on 20 Jan 2012, 03:35 PM
ClaimedAmount = Decimal
0
Marcos Mataloni
Top achievements
Rank 1
answered on 20 Jan 2012, 04:09 PM
Sorry, I've the same problem, but even if I set the 'SourceField' property, I fall in exception.
I think that tis behaviour is related to crazy result I have with 'First' and 'Last': both return the 'System.Data.DataRow' and not the first and last value respectively.
In summary: Min, Max, Count are ok, First and Last are not usable, while Sum and Average crash!!!
Are there any solutions?
Thank's
:-)
0
Vlad
Telerik team
answered on 23 Jan 2012, 08:32 AM
Hello,

 Have you tried the DataView of this DataTable?

Regards,
Vlad
the Telerik team

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

0
Oliver
Top achievements
Rank 1
answered on 26 Jan 2012, 05:38 PM
Hi,

the ItemsSource of my RadGridView it's an array of object.

I sill falling in an exception when I use the sum/average aggregate functions. "Aucune méthode 'Sum' sur le type 'System.Linq.Enumerable' n'est compatible avec les arguments fournis."

I also have a problem with First/Last aggregation functions. Look at the screenshot.

Thank's
0
Oliver
Top achievements
Rank 1
answered on 01 Mar 2012, 07:55 PM
Hi,

to see a concrete situation, just put the indicated lines in comment or not to see the application working or crash :)
PS: Also, look at the First/Last aggregate functions UI result !!!


XAML
*******************************************
<Window x:Class="WpfApplication3.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded" WindowState="Maximized">
    <Grid>
        <telerik:RadGridView Name="gridViewTest" AutoGenerateColumns="False" RowIndicatorVisibility="Collapsed" ShowColumnFooters="True"/>
    </Grid>
</Window>
*******************************************
C#
*******************************************


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Telerik.Windows.Controls;
using Telerik.Windows.Data;

namespace WpfApplication3
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<Person> allPersons = new List<Person>();

            createGridColumns();

            allPersons.Add(new Person() { LastName = "Doe", FirstName = "John", Number1 = 1, Number2 = 2, Number3 = 3 });
            allPersons.Add(new Person() { LastName = "Fraser", FirstName = "Joe", Number1 = 4, Number2 = 2, Number3 = 6 });
            allPersons.Add(new Person() { LastName = "Alban", FirstName = "Ricardo", Number1 = 7, Number2 = 8, Number3 = 9 });

            gridViewTest.ItemsSource = allPersons;
        }

        private void createGridColumns()
        {
            GridViewDataColumn col1 = new GridViewDataColumn();
            GridViewDataColumn col2 = new GridViewDataColumn();
            GridViewDataColumn col3 = new GridViewDataColumn();
            GridViewDataColumn col4 = new GridViewDataColumn();
            GridViewDataColumn col5 = new GridViewDataColumn();

            col1.Header = "LastName";
            col1.UniqueName = "col1";
            col1.DataMemberBinding = new Binding("LastName");
            col1.AggregateFunctions.Add(new CountFunction() { Caption = "Count: " });

            col2.Header = "FirstName";
            col2.UniqueName = "col2";
            col2.DataMemberBinding = new Binding("FirstName");
            col2.AggregateFunctions.Add(new FirstFunction() { Caption = "First: " });
            col2.AggregateFunctions.Add(new LastFunction() { Caption = "Last: " });
            col2.AggregateFunctions.Add(new MinFunction() { Caption = "Min: " });
            col2.AggregateFunctions.Add(new MaxFunction() { Caption = "Max: " });

            col3.Header = "Number1";
            col3.UniqueName = "col3";
            col3.DataFormatString = "C2";
            col3.DataMemberBinding = new Binding("Number1");
            col3.AggregateFunctions.Add(new MinFunction() { Caption = "Min: " });
            col3.AggregateFunctions.Add(new MaxFunction() { Caption = "Max: " });

            col4.Header = "Number2";
            col4.UniqueName = "col4";
            col4.DataFormatString = "C2";
            col4.DataMemberBinding = new Binding("Number2");
            col4.AggregateFunctions.Add(new AverageFunction() { Caption = "Avg: ", SourceFieldType=typeof(decimal) }); // *** Put this line in comment to have a working app ***

            col5.Header = "Number3";
            col5.UniqueName = "col5";
            col5.DataFormatString = "C2";
            col5.DataMemberBinding = new Binding("Number3");
            col5.AggregateFunctions.Add(new SumFunction() { Caption = "Sum: ", SourceFieldType = typeof(decimal) }); // *** Put this line in comment to have a working app ***

            gridViewTest.Columns.Add(col1);
            gridViewTest.Columns.Add(col2);
            gridViewTest.Columns.Add(col3);
            gridViewTest.Columns.Add(col4);
            gridViewTest.Columns.Add(col5);
        }
    }

    public class Person
    {
        public object LastName { get; set; }
        public object FirstName { get; set; }
        public object Number1 { get; set; }
        public object Number2 { get; set; }
        public object Number3 { get; set; }
    }
}

*******************************************


Thank's
Tags
GridView
Asked by
Oliver
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Oliver
Top achievements
Rank 1
Marcos Mataloni
Top achievements
Rank 1
Share this question
or