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

Additional sorting column

3 Answers 130 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Pero
Top achievements
Rank 1
Pero asked on 10 Aug 2016, 02:05 PM
Hi, I would like to set column sorting based on other column from grid. Please look on attached picture (Table.png) where you can see simple definition (I have column "Desc1" and primary key "SecMainClass". For header on column "Desc1" I'm using resource "Main classification"). I want use sorting for column "Desc1" based on primary key "SecMainClass". I saw that I can use property "SortMemberPath" for additional sorting definition and that works fine if I don't have grouping. When I put column "Desc1" as grouped column, grid use default ("alphabetically") sorting. For grouping I'm using new "ColumnGroupDescriptor". Before I decide ask you, I saw that I can use custom group descriptor....Please look attached picture (CustomGrouping.png). As you can see I was create custom grouping descriptor and sorting works fine on grouping too but in that case grid doesn't show totals on grouping header for other columns....(picutre "TotalsON" is with normal GroupColumnDescriptor where totals are showed and picture "TotalsOFF" is with custom grouping descriptor where totals missing). My question is: How I can use custom sorting on ColumnGroupDescriptor? Can you help me and tell me it is possible to apply "GroupSortingExpression" from "custom grouping descriptor" to "ColumnGroupDescriptor" with method "CreateGroupSortExpression"? Can you send me example how I can use that method because I didn't find any example on your documentation or foru?. Do you have maybe any other solution for situation like this? My version is : 2016.2.613.45. Thank you very much for your help. I'm looking forward to hearing from you.

3 Answers, 1 is accepted

Sort by
0
Stefan Nenchev
Telerik team
answered on 12 Aug 2016, 11:29 AM
Hello Pero,

I am not sure of your exact setup but the following thread from our forum should be of help - Customize Group and Sort. I have created a sample project based on the suggestions there and it seems to work as desired. Please review it. Furthermore, you can check the "Sort Group by Aggregate" demo in our SDK Samples Browser where custom sorting on groups is shown.

Regards,
Stefan Nenchev
Telerik by Progress
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Pero
Top achievements
Rank 1
answered on 14 Aug 2016, 11:09 AM
Dear Mr. Nenchev, thank you very much for your solution. Unfortunatelly, your solution don't help me because I already know everthying which you send me in your example. I will try explain you my question based on your solution. Is it possible to use on the event "clubsGrid_Grouping" ColumnGroupDescriptor for the column "Name", not custom descriptor "GroupDescriptor<Club, string, int>" but with still sorting by "Stadium capacity"? Can you show me if is possible to apply properties "GroupingExpression" and "GroupSortingExpression" from custom descriptor to ColumnGroupDescriptor on grouping event (in your case on the event "clubsGrid_Grouping")? I need ColumnGroupDescriptor because if you set aggregate sum function on the column "Stadium capacity", totals are not showed when you use custom group descriptor? Can you show me how I can use ColumnGroupDescriptor on the column "Name" but with additional sorting column (in your case by column "Stadium capacity")?
0
Stefan Nenchev
Telerik team
answered on 16 Aug 2016, 08:20 AM
Hi Pero,

The easiest approach is to create a Custom AggregateFunction and add it to the generic GroupDescriptor`s AggregateFunctions collection. So, in the sample project that I have sent, you can create a custom aggregate function that returns the sum of  the stadium capacities:

class CustomAggregateFunction : AggregateFunction<Club, int>
   {
       public CustomAggregateFunction()
       {
           this.AggregationExpression = items => CountStadiumSum(items);
       }
 
       private int CountStadiumSum(IEnumerable<Club> items)
       {
           var itemCount = items.Count();
           if (itemCount > 0)
           {
               var values = items.Select(i => i.StadiumCapacity);
               var sum = values.Sum();
               return sum;
           }
           return 0;
       }
   }

And in the grouping event:

private void clubsGrid_Grouping(object sender, GridViewGroupingEventArgs e)
       {
           if (e.Action == GroupingEventAction.Place && (e.GroupDescriptor as ColumnGroupDescriptor).Column.Header.ToString() == "Name")
           {
               e.Cancel = true;
               var descriptor = new GroupDescriptor<Club, string, int>
               {
                   GroupingExpression = i => i.Name,
                   GroupSortingExpression = i => i.FirstOrDefault().StadiumCapacity
               };
               descriptor.DisplayContent = ((Telerik.Windows.Data.GroupDescriptorBase)(e.GroupDescriptor)).DisplayContent;
               descriptor.SortDirection = e.GroupDescriptor.SortDirection;
               descriptor.AggregateFunctions.Add(new CustomAggregateFunction());
               this.clubsGrid.GroupDescriptors.Add(descriptor);
           }
       }

Would this work for you?

Regards,
Stefan Nenchev
Telerik by Progress
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
GridView
Asked by
Pero
Top achievements
Rank 1
Answers by
Stefan Nenchev
Telerik team
Pero
Top achievements
Rank 1
Share this question
or