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

Adding aggregate function dynamically crass my application.

9 Answers 183 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Oliver
Top achievements
Rank 1
Oliver asked on 02 Mar 2012, 08:59 PM
Hi,
 I'm using Telerik V2011.3.1220.35 and. I create some aggregate functions dynamically to my grid. The sum/average functions cause my application to crash. To see a concrete situation, just comment or uncomment the indicated lines to see the application working or crashing :)

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

9 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 05 Mar 2012, 08:25 AM
Hello,

 You've not specified SourceField for these functions. Please refer to our demos for more info! :)

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
Oliver
Top achievements
Rank 1
answered on 05 Mar 2012, 03:19 PM
Hi,

sorry but having a SourceField=something soesn't change anything :(

ie:

col4.AggregateFunctions.Add(

new AverageFunction() { Caption = "Avg: ", SourceField = "Number2", SourceFieldType = typeof(decimal) });

Thank's

0
Vlad
Telerik team
answered on 05 Mar 2012, 03:36 PM
Hello,

 I've missed the fact that your Number2 field is not numeric - you can sum only numeric types. 

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
Oliver
Top achievements
Rank 1
answered on 05 Mar 2012, 04:01 PM
Even if I specify the SourceFieldType???
0
Vlad
Telerik team
answered on 05 Mar 2012, 04:05 PM
Hello,

 Even in this case. This property is to help the grid if we cannot discover the type automatically. You cannot sum objects. 

Greetings,
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
Oliver
Top achievements
Rank 1
answered on 05 Mar 2012, 05:49 PM
Eveb if I implement a CustomAggregate???

Thank's
0
Vlad
Telerik team
answered on 06 Mar 2012, 08:12 AM
Hello,

 If you implement custom aggregate you will have custom code where you can cast this object to an actual numeric type. 

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
Oliver
Top achievements
Rank 1
answered on 06 Mar 2012, 03:05 PM
Hi Vlad,

here what I did but it sill doesn't work :(

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: ", SourceField = "Number2", 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 ***
            col5.AggregateFunctions.Add(new SumSumFunction() { Caption = "Sum: ", SourceField="Number3", SourceFieldType = typeof(double) }); // *** 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; }
    }

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

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

    public static class Sum
    {
        public static double SumSumFunction<TSource>(IEnumerable<TSource> source, Func<TSource, double> selector)
        {
            IEnumerable<double> values = from i in source select Convert.ToDouble(selector(i));           
            return sum(values);
        }

        private static double sum(IEnumerable<double> values)
        {
            double sumTot = 0;

            foreach (double val in values)
            {
                sumTot += val;
            }

            return sumTot;
        }
    }
}


Thank's
0
Dimitrina
Telerik team
answered on 07 Mar 2012, 01:21 PM
Hi,

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

Kind 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
Oliver
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Oliver
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or