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

Automatic AggregateFunction on groups

10 Answers 134 Views
GridView
This is a migrated thread and some comments may be shown as answers.
JDB
Top achievements
Rank 1
JDB asked on 25 Mar 2009, 11:02 AM
I'm trying to get the RadGridView for WPF to work in our software, and I was wondering if it is possible to show the Item count of a group in the group header without using predefined GroupDescriptions and preferably a XAML-only solution.

So far, all the examples on AggregateFunctions I have found, either have predefined GroupDescriptions in XAML or set the AggregateFunction programmatically using a hardcoded GroupDescription.

Edit:

I managed to fake the desired functionality by using the GroupDescriptions.CollectionChanged event:

 
private void GroupDescriptions_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)  
    {  
      if (recordings.GroupDescriptions.Count > 0)  
      {  
        foreach (RadGroupDescription rgd in recordings.GroupDescriptions)  
        {  
          CountFunction cf = new CountFunction(rgd.PropertyName, rgd.PropertyName, "");  
          rgd.AggregateFunctions.Add(cf);  
        }  
      }  
    } 

Strangely, the e.Action is never Add when dragging a column to the GroupPanel, so I had to do it this way.

 

 

10 Answers, 1 is accepted

Sort by
0
Hristo Deshev
Telerik team
answered on 27 Mar 2009, 04:32 PM
Hello RVW,

Glad you got this working. If I understand correctly, you want to enforce a Count aggregate function for all the groups a user defines by dragging/dropping. At the moment your solution seems to be the only one. We may provide a full-blown event on RadGridView, something like GroupsChanged, so that users have a better place to hook to.

We reproduced the Add action never firing problem, and we are looking into it. Thanks for reporting it -- I have updated your Telerik points.

Sincerely yours,
Hristo Deshev
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
JDB
Top achievements
Rank 1
answered on 30 Mar 2009, 06:06 AM
Hi Hristo,

Thanks for you response.

You understood correctly, the user wants to know how many items are in a group, so I need a Count aggregate.

The only problem I have with this solution, is the InheritedAggregates. What happens is the following:

Group 1: 100 items
- Group 1.1: 60 items | 60 items
- - Group 1.1.1: 20 items | 20 items | 20 items
- Group 1.2: 40 items | 40 items

etc

I tried clearing the InheritedAggregates collection after adding the new CountAggregate, but that doesn't do anything.
0
Accepted
Hristo Deshev
Telerik team
answered on 30 Mar 2009, 11:42 AM
Hi RVW,

Instead of trying to clear the inherited aggregate functions, can't you just add aggregates for the first (topmost) group only? I am thinking that code like the one below should do the trick:

        private void GroupDescriptions_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
        { 
            if (RadGridView1.GroupDescriptions.Count > 0) 
            { 
                var firstGroupDescription = RadGridView1.GroupDescriptions.FirstOrDefault(); 
                if (firstGroupDescription != null && firstGroupDescription.AggregateFunctions.Count == 0) 
                { 
                    CountFunction cf = new CountFunction(firstGroupDescription.PropertyName, firstGroupDescription.PropertyName, ""); 
                    firstGroupDescription.AggregateFunctions.Add(cf); 
                } 
            } 
        }  

It generates a Count function for the topmost group and it gets inherited for the nested group thus generating only one aggregate cell per group header.

I am attaching my test project for your reference.

All the best,
Hristo Deshev
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
JDB
Top achievements
Rank 1
answered on 30 Mar 2009, 11:47 AM
Hi Hristo,

Thanks for your answer, it works like a charm!
0
JDB
Top achievements
Rank 1
answered on 09 Jul 2009, 07:08 AM
Hi,

With the changes in the 2009Q2 version, this solution no longer works. The column and group footers are nice, but aren't what my customers want. So far I haven't been able to programmatically add a new aggregate countfunction.

Could you give me an example of how to do this with the recent changes?

Thanks in advance
0
Vlad
Telerik team
answered on 09 Jul 2009, 02:26 PM
Hello RVW,

I've attached modified version of the project to illustrate you how to achieve your goal with the new grid version.

Let me know how it goes.

All the best,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
JDB
Top achievements
Rank 1
answered on 10 Jul 2009, 12:54 PM
Hi Vlad,

Thanks for the example. The weird thing is, that while your example works perfectly, I can't get it to work in my own project. The event is attached in the window constructor, like in your example. The code to add the aggregate countfunction is the same. But even if I put a breakpoint in the code, the debugger won't stop there!

I see no difference in the usage of the RadGridView control, except for some different properties that have nothing to do with grouping at all.

By the way, is it possible that the Grouped event is raised too soon, just like the Filtered event? I think it is, the Record collection doesn't contain the correct items until after the Grouped event.
0
Vlad
Telerik team
answered on 13 Jul 2009, 05:45 AM
Hello RVW,

Can you send us small project where we can reproduce and debug this?

Greetings,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
JDB
Top achievements
Rank 1
answered on 13 Jul 2009, 08:48 AM

Hi Vlad,

I've managed to make it reproducable. In your example you load and set the itemssource in the constructor.

I've added a button, and in the Click event, I set the itemssource and removed the code from the constructor:

public Window1()  
    {  
      InitializeComponent();  
 
      RadGridView1.Grouping += RadGridView1_Grouping;  
    }  
 
    void RadGridView1_Grouping(object sender, Telerik.Windows.Controls.GridView.GridView.GridViewGroupingEventArgs e)  
    {  
      if (e.GroupDescriptor != null && e.GroupDescriptor.AggregateFunctions.Count == 0)  
      {  
        e.GroupDescriptor.AggregateFunctions.Add(new CountFunction());  
      }  
    }  
 
 
    private void Button_Click(object sender, RoutedEventArgs e)  
    {  
      RadGridView1.ItemsSource = new Message[] {   
                new Message { Sender = "tom@hanna-barbera.com"Subject = "Cats are cool"Size = 100 },  
                new Message { Sender = "jerry@hanna-barbera.com"Subject = "Mice are cool"Size = 100 },  
                new Message { Sender = "spike@hanna-barbera.com"Subject = "Dogs are cool"Size = 100 }  
            };  
    } 

The Grouping event is not raised if you do this!
0
Vlad
Telerik team
answered on 16 Jul 2009, 09:32 AM
Hi RVW,

This is definitely a bug and will be fixed immediately. I've added 2000 Telerik points to your account!

This fix will be part of our latest internal release tomorrow.

Regards,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
JDB
Top achievements
Rank 1
Answers by
Hristo Deshev
Telerik team
JDB
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or