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

Different aggregate functions on different columns

13 Answers 412 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Inger Marie
Top achievements
Rank 1
Inger Marie asked on 28 Feb 2017, 08:31 AM

It seems like if I add different aggregate functions to the different columns, all columns gets all aggregate functions. Is there any way to prevent this?

 

(For instance - I would like some of the columns group header to include the column header - but not for all columns)

 

Thanks

Inger Marie

 

13 Answers, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 02 Mar 2017, 03:04 PM
Hello Inger Marie,

Unfortunately, I am not able to completely understand your requirement. Can you please elaborate a bit on that all columns are getting all aggregate functions? If I am not missing anything, you need to have an AggregateFunction defined, but not displayed for a given column. Can you please confirm this?

Thank you in advance for your cooperation.

Regards,
Stefan X1
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Inger Marie
Top achievements
Rank 1
answered on 03 Mar 2017, 07:41 AM

I am sorry - I was a bit vague. I solved the thing with the column group header (using different header group templates on different columns), but I still hasn't been able to get the columns to show different aggregate functions.

The aggregate functions gets added to a column through GridViewDataColumn.AggregateFunctions - but it still applies to all column in the grid. Which is good for the most part because normally I only want to count the number of items in each group. But occationally I want the aggregate functions to be different for a few columns.

An example: Let's say I do this:

<telerik:GridViewDataColumn x:Name="NameColumn" IsReadOnly="True"
    Header="Name"
    Width="SizeToHeader"
    DataMemberBinding="{Binding Name}" >
    <telerik:GridViewDataColumn.AggregateFunctions>
        <telerik:CountFunction  Caption="Count"   ResultFormatString="{}: {0:#,0}" />
    </telerik:GridViewDataColumn.AggregateFunctions>
 </telerik:GridViewDataColumn>
 
     <telerik:GridViewDataColumn x:Name="PriceColumn" IsReadOnly="True"
    Header="Price"
    Width="SizeToHeader"
    DataMemberBinding="{Binding Price}"
    GroupMemberPath="PriceGroup">
    <telerik:GridViewDataColumn.AggregateFunctions>
        <telerik:AverageFunction  Caption="Avg"  ResultFormatString="{}: {0:D2}"  />
     </telerik:GridViewDataColumn.AggregateFunctions>
</telerik:GridViewDataColumn>
 
<telerik:GridViewDataColumn x:Name="DiscountColumn" IsReadOnly="True"
    Header="Discount"
    Width="SizeToHeader"
    DataMemberBinding="{Binding Discount}"
    GroupMemberPath="DiscountGroup">
    <telerik:GridViewDataColumn.AggregateFunctions>
        <telerik:MinFunction  Caption="Min" ResultFormatString="{}: {0:D2}" />
        <telerik:MaxFunction  Caption="Max" ResultFormatString="{}: {0:D2}" />
    </telerik:GridViewDataColumn.AggregateFunctions>
  </telerik:GridViewDataColumn>

 

But I would like the Price column to have only the Average-Function - not the count or min or max.

(see the attached picture)

How do I do that?
0
Stefan
Telerik team
answered on 07 Mar 2017, 01:24 PM
Hello Inger Marie,

Thank you for the clarification.

A possible approach to conditionally add/remove aggregate functions based on the column by which the control is grouped is to hook to its Grouping event. Through the event arguments you can determine by which column the grouping operation is performed and implement the needed logic. You can check out the following code snippet as a reference.
private void clubsGrid_Grouping(object sender,
    GridViewGroupingEventArgs e)
{
    var column = (e.GroupDescriptor as ColumnGroupDescriptor).Column;
    if (column.UniqueName == "StadiumCapacity")
    {
        column.AggregateFunctions.Add(new SumFunction());
    }
}

Can you please give the suggestion a try? Is this the behavior of the control you are aiming at?

All the best,
Stefan X1
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Inger Marie
Top achievements
Rank 1
answered on 13 Mar 2017, 02:37 PM

No. It works fine until I start grouping by both columns. Or if I group by one, remove the grouping and then group by the other.

 

If I do this:

private void ClubsGrid_OnGrouping(object sender, GridViewGroupingEventArgs e)
      {
          var column = (e.GroupDescriptor as ColumnGroupDescriptor)?.Column;
          if (column == null)
              return;
          if (column.Header.ToString() == "Est.")
          {
              column.AggregateFunctions.Add(new CountFunction() {Caption =  "Count:"});
          }
          if (column.Header.ToString() == "Stadium")
          {
              column.AggregateFunctions.Add(new SumFunction() {Caption =  "Sum:"});
          }
      }

 

Then both Est. and Stadium ends up with both aggregate functions if I group by both.

I want Est to have a count function, but not a sum. And Stadium to have a sum function but not a count when I group first by Est and then by Stadium.

Even though I add the CountFunction to the Est-column - it seems to be a grid-wide setting on all column headers.
0
Stefan
Telerik team
answered on 16 Mar 2017, 09:44 AM
Hello Inger Marie,

The example provided was intended to serve just as a demonstration on which the logic for adding/removing the needed AggregateFunctions can be based. Please, excuse me if my reply was not clear enough regarding this. So, based on the column on which the control is being grouped, you need to manually add or remove the relevant AggregateFunctions.

Hopefully, this clarifies your concern. In case further assistance is needed, feel free to approach me.

Best Regards,
Stefan X1
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Inger Marie
Top achievements
Rank 1
answered on 16 Mar 2017, 10:42 AM

I understood it was an example. Problem is that I do not know how to make that work. If I do this:

private void ClubsGrid_OnGrouping(object sender, GridViewGroupingEventArgs e)
       {
           var column = (e.GroupDescriptor as ColumnGroupDescriptor)?.Column;
           if (column == null)
               return;
           if (column.Header.ToString() == "Est.")
           {
               column.AggregateFunctions.Clear();
               column.AggregateFunctions.Add(new CountFunction() {Caption =  "Count:"});
           }
           if (column.Header.ToString() == "Stadium")
           {
               column.AggregateFunctions.Clear();
               column.AggregateFunctions.Add(new SumFunction() {Caption =  "Sum:"});
           }
       }

 

I still get both count and sum functions on both column headers (if I group on both columns).

 

0
Stefan
Telerik team
answered on 21 Mar 2017, 07:53 AM
Hi Inger Marie,

Thanks for the update.

Instead of clearing the AggregateFunctions of the column through which RadGridView is currently grouped, you need to ensure that the AggregateFunctions of the other columns are cleared. This can be achieved by iterating over the Columns collection of the control.
private void clubsGrid_Grouping(object sender, GridViewGroupingEventArgs e)
        {
            var column = (e.GroupDescriptor as ColumnGroupDescriptor).Column;
            if (column.UniqueName == "StadiumCapacity")
            {
                foreach (var col in this.clubsGrid.Columns)
                {
                       col.AggregateFunctions.Clear();
                }
                column.AggregateFunctions.Add(new SumFunction());
            }
            else if (column.UniqueName == "FanClubCount")
            {
                foreach (var col in this.clubsGrid.Columns)
                {
                       col.AggregateFunctions.Clear();
                     
                }
                column.AggregateFunctions.Add(new SumFunction());
            }
        }

Can you please give it a try? Is this the behavior you are aiming at?

Best Regards,
Stefan X1
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Inger Marie
Top achievements
Rank 1
answered on 21 Mar 2017, 12:58 PM

This does not work either - not when grouping by multiple columns.

I want the Stadium column group header to show a sum - and always a sum and only a sum.

I want the Est. column group header to show a count - and always a count and only a count.

The proposed solution will give both column group headers the aggregate function of whatever column was grouped last (when I group with first one and then the other).

The problem seems to be that whatever aggregate function gets selected on one column also gets activated on all the other column group headers. 

So removing an aggregate function on one column group header would remove it on all column group headers.

 

0
Stefan
Telerik team
answered on 24 Mar 2017, 08:50 AM
Hi Inger Marie,

Thank you for the clarification.

It seems that I have misunderstood you. Please, excuse me for this. The built in mechanism that the control provides for this requirement is to use the ShowGroupHeaderColumnAggregates property of GroupHeaderRow. By setting it to true, the aggregates will be aligned to the relevant column. More information can be found in the Align Header Aggregates section of the Group Aggregates topic as well in the Aggregates WPF demo.

Can you please check it out? Do let me know in case further assistance is needed.

Best Regards,
Stefan X1
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Inger Marie
Top achievements
Rank 1
answered on 24 Mar 2017, 12:02 PM

I do not want to align the group header.

I do not know how to explain what I want in any better terms.

Says I have column X which I want to aggregate by count, and column Y which I want to aggregate by average.

Then I want this:

Group X1: Count=#

     

0
Inger Marie
Top achievements
Rank 1
answered on 24 Mar 2017, 12:04 PM

(I was not done)

 

Group X1: Count # (NO AVERAGE)

  Group Y1: Average ## (NO COUNT)

  Group Y2: Average ## (NO COUNT)

Group X2: Count # (NO AVERAGE)

  Group Y1: Average ## (NO COUNT)
  Group Y2: Average ## (NO COUNT)

0
Accepted
Stefan
Telerik team
answered on 29 Mar 2017, 11:23 AM
Hello Inger Marie,

Thanks for this additional clarification.

Unfortunately, the control is not designed to support such conditional applying of its aggregate results. The GridViewGroupRow/GroupHeaderRow(this depends on whether Flat or Nested GroupRenderMode of RadGridView is used) populates the whole AggregateResults collection. I am afraid, that there is no way to conditionally set which aggregate result to be displayed by a particular row. You can either display them all or hide them.

So, the only possible solution that I can propose you, is to try editing the ControlTemplate of GridViewGroupRow/GroupHeaderRow and try to apply the needed customization. I assume that it would be helpful to know, that the DataContext of both of them is the GroupViewModel class. Note, that as this is a customization of the by-design implementation, I cannot confirm that a hundred percent working solution behaving as expected in all scenarios can be implemented.

Hopefully, I have managed to clarify your concerns.

Best Regards,
Stefan X1
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Inger Marie
Top achievements
Rank 1
answered on 29 Mar 2017, 11:29 AM

Ok, this is kind of what I thought.

It is confusing that the agregates are applied to columns, but works on the entire grid. They really should be applied to the RadGridView-control for the design (xaml) to match the functionality (like rowstyles are).

Thank you for clarifying this!

 

Tags
GridView
Asked by
Inger Marie
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Inger Marie
Top achievements
Rank 1
Share this question
or