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

CalculateAggregates does not update the AggregateFunctions

2 Answers 129 Views
GridView
This is a migrated thread and some comments may be shown as answers.
kingnight
Top achievements
Rank 1
kingnight asked on 09 Jan 2013, 05:00 AM
I am using AggregateFunctions in GridView, but the function will not recalculate automatically when the data update.
I  tried to manually call CalculateAggregates(), but still not working.
I spent a lot of time to search for that, and found that a lot of people have similar problems. But I did't find a good solution.

The only way I know is to use Rebind() after the data updates. I think there should be some better ways.


I created a sample project here. If click the button, it will increase the number by one. If I group it by Position, and click the button, the number data value changes, but the  sum value does not.
Could someone help me with it please?

Thanks very much.



<Window x:Class="SampleProject.MainWindow"
  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"
        xmlns:my="clr-namespace:SampleProject"
        Title="MainWindow" Height="700" Width="700">
 <Window.Resources>
  <my:MyViewModel x:Key="myViewModel"/>
 </Window.Resources>
    <Grid DataContext="{StaticResource myViewModel}">
  <Grid.RowDefinitions>
   <RowDefinition Height="*"/>
        
   <RowDefinition Height="Auto" />
  </Grid.RowDefinitions>
        <telerik:RadGridView Name="playersGrid" Grid.Row="0"
                                     ItemsSource="{Binding Players}"
                                     AutoGenerateColumns="False">  
   <telerik:RadGridView.Columns>
    <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Position}"/>
    <telerik:GridViewDataColumn DataMemberBinding="{Binding Number}">
     <telerik:GridViewDataColumn.AggregateFunctions>
                        <telerik:SumFunction SourceField="Number"  Caption="Column count: " />
     </telerik:GridViewDataColumn.AggregateFunctions>
    </telerik:GridViewDataColumn>
    <telerik:GridViewDataColumn DataMemberBinding="{Binding Country}"/>
   </telerik:RadGridView.Columns>
  </telerik:RadGridView>
    
        <StackPanel Grid.Row="1" Orientation="Horizontal">
   <telerik:RadButton Name="Button1"
                Content="Update Data"
                Click="Button_Click"
                Margin="5"
                HorizontalAlignment="Left"/>
        
  </StackPanel>
 </Grid>
</Window>









2 Answers, 1 is accepted

Sort by
0
kingnight
Top achievements
Rank 1
answered on 09 Jan 2013, 05:01 AM

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;

namespace SampleProject
{
 /// <summary>
 /// Interaction logic for MainWindow.xaml
 /// </summary>
 public partial class MainWindow : Window
 {
  

  public MainWindow()
  {
   InitializeComponent();
   
  }

  private void Button_Click(object sender, RoutedEventArgs e)
  {
            var Players = (ObservableCollection<Player>)playersGrid.ItemsSource;
            foreach (var player in Players)
            {
                player.Number++;
            }
            playersGrid.CalculateAggregates();
        //    playersGrid.Rebind();

  }
  
 }
}

0
kingnight
Top achievements
Rank 1
answered on 09 Jan 2013, 05:02 AM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace SampleProject
{
 /// <summary>
 /// A football player.
 /// </summary>
 public class Player : INotifyPropertyChanged
 {
  public event PropertyChangedEventHandler PropertyChanged;

  private string name;
  private int number;
  private Position position;
  private string country;

  public string Name
  {
   get { return this.name; }
   set
   {
    if (value != this.name)
    {
     this.name = value;
     this.OnPropertyChanged("Name");
    }
   }
  }

  public int Number
  {
   get { return this.number; }
   set
   {
    if (value != this.number)
    {
     this.number = value;
     this.OnPropertyChanged("Number");
    }
   }
  }

  public Position Position
  {
   get { return this.position; }
   set
   {
    if (value != this.position)
    {
     this.position = value;
     this.OnPropertyChanged("Position");
    }
   }
  }

  public string Country
  {
   get { return this.country; }
   set
   {
    if (value != this.country)
    {
     this.country = value;
     this.OnPropertyChanged("Country");
    }
   }
  }

  public Player()
  {

  }

  public Player(string name, int number, Position position, string country)
  {
   this.name = name;
   this.number = number;
   this.position = position;
   this.country = country;
  }

  protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  {
   PropertyChangedEventHandler handler = this.PropertyChanged;
   if (handler != null)
   {
    handler(this, args);
   }
  }

  private void OnPropertyChanged(string propertyName)
  {
   this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  }

  public override string ToString()
  {
   return this.Name;
  }

  public static ObservableCollection<Player> GetPlayers()
  {
   return new ObservableCollection<Player>(Club.GetClubs().SelectMany(c => c.Players));
  }
 }
}

Tags
GridView
Asked by
kingnight
Top achievements
Rank 1
Answers by
kingnight
Top achievements
Rank 1
Share this question
or