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

Sorting the groups

7 Answers 549 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 18 Aug 2014, 08:50 AM
Hi

I have a gridview which is supposed to show the Nace codes of companies. A Nace Code consists of two strings: The Code and the Name (the Code is a number, but it's type is a string).

In the cells, I want to write: "Code: Name". When the user sorts this column, I want the sorting to be on the Code.
For instance:
3012: Boots- und Yachtbau
9311: Betrieb von Sportanlagen

When the nace code is grouped, I want the headers to be "Name (Code)". When the user sorts the groups, I want the sorting to be on Name:
For instance:
Bibliotheken und Archive (9101)
Boots- und Yachtbau (3012)

I have an Entity with a property named NaceCodeObject which has 2 properties: NaceCode and IndustryName

I can create a GridViewDataColumn which - by the way of a converter and concarding the two properties into a single string - shows me the data I want in the Group header and in the cell. But it seems like SortMemberPath uses the value DataMemberBinding, not the value of the group header for sorting the groups.

How can I solve this problem? Is there any way to sort a column one way when just displaying the rows and another way when grouping?

7 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 18 Aug 2014, 01:08 PM
Hello,

Specifying a SortMemberPath for the column will be respected when sorting the column itself. If you sort the group, then the sorting will be done based on the DataMemberBinding.

In order to have the sorting through the group work differently, you can replace the default ColumnGroupDescriptor with a generic GroupDescriptor. 

For the purpose, you need to subscribe for the Grouping event of RadGridView to cancel the default grouping first. Then you should create and apply the one to perform the sorting operations you need by specifying a proper GroupSortingExpression
For example:
private void clubsGrid_Grouping(object sender, GridViewGroupingEventArgs e)
{
    if(e.Action == GroupingEventAction.Place)
    {
        var columnGroup = e.GroupDescriptor as ColumnGroupDescriptor;
        if (columnGroup != null && columnGroup.Column.UniqueName == "Name")
        {
            // cancel the default grouping
            e.Cancel = true;
 
            // define a generic GroupDescriptor
            var descriptor = new GroupDescriptor<Club, stringint>
            {
                GroupingExpression = o => o.Name,
                GroupSortingExpression = group => group.ElementAt(0).StadiumCapacity,
                SortDirection = e.GroupDescriptor.SortDirection,
                DisplayContent = "Name"
            };
            // add the new descriptor
            clubsGrid.GroupDescriptors.Add(descriptor);
        }
    }
}

You can also check our online documentation on Using generic GroupDescriptor.

Let me know how this works for you.

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Inger Marie
Top achievements
Rank 1
answered on 19 Aug 2014, 06:54 AM
Thanks for your reply. I appresiate the help.

What I have is a Company object which has a Nace Code object which has two string properties (key and name).Like this:
public class Company
{
    public NaceCode NaceCode {get; set;}
}

public class NaceCode
{
    public string Key{get; set;}
    public string Name{get; set;}
}

My columns are showing the Company (name, contact person etc etc). The ItemsSource of my RadGridView is an observablecollection of Companies.

I want a single Nace Code column which sorts by key (string) first and industry (string) last. But when I group it I want it to sort by industry (first) and key (string) last.

When I do this in xaml: GroupMemberPath="Company.NaceCode", then it will not group at all.
When I do this GroupMemberPath="Company.NaceCode.Key" it groups but not correctly.

This is my XAML (which wont group - I get some red icon when I try to drag the column header):

 <telerik:GridViewDataColumn x:Name="_naceColumn"
                                            Header="Nace Code"
                                            DataMemberBinding="{Binding Company.NaceCode, Converter={StaticResource NaceCodeLongFormatConverter}, ConverterParameter={x:Static Member=Converters:NaceCodeLongFormatConverterParameter.KeyFirst}}"
                                             IsReadOnly="True"
                                            IsGroupable="True"
                                            Width="SizeToHeader"
                                            SortMemberPath="Entity.SupplierNaceCode.Key"
                                           ToolTipService.ShowOnDisabled="True">

This is my codebehind (the Companies_Grouping does not get fired because I am not allowed to group):

public <constructor>
        {
            InitializeComponent();
            <...>

            this._companies.Grouping += Companies_Grouping;
        }

  void Companies_Grouping(object sender, GridViewGroupingEventArgs e)
        {
            if (e.Action == GroupingEventAction.Place)
            {
                var columnGroup = e.GroupDescriptor as ColumnGroupDescriptor;
                if (columnGroup != null && columnGroup.Column.UniqueName == "_naceColumn")
                {
                    // cancel the default grouping
                    e.Cancel = true;

                    // define a generic GroupDescriptor
                    var descriptor = new GroupDescriptor<NaceCode, String, String>
                    {
                        GroupingExpression = o => String.Format("{0} ({1})", o.IndustryName, o.Key),
                        GroupSortingExpression = group => group.ElementAt(0).IndustryName,
                        SortDirection = e.GroupDescriptor.SortDirection,
                        DisplayContent = "Name" // ??
                    };
                    // add the new descriptor
                    _companies.GroupDescriptors.Add(descriptor);
                }
            }
        }

Question: What is wrong with my xaml since I cannot group. How to I manage to get a NaceCode-object as the Group.Key ?

Thanks,

0
Dimitrina
Telerik team
answered on 19 Aug 2014, 08:01 AM
Hi,

Basically grouping in RadGridView is a data operation. The default grouping occurs as the user drags a column header and drops it into the GroupPanel. Then, we internally generate and execute a LINQ query appending a GroupBy clause to the source collection.
If this query executes fine, then the operation is successful and the data is grouped. Otherwise, this operation is not allowed.

May I ask you to perform a simple test - try to manually build and execute such a GroupBy query on the property you group on? Does such a query pass correctly?

If no, then I am afraid it is not possible to group on the property specified.

If yes, then it may be a problem with our implementation. In that case, may I ask you to isolate the issue in a demo project and send it to me?

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Heiko
Top achievements
Rank 1
answered on 07 Apr 2015, 08:08 AM

Hi Dimitrina,and thank you for the post. Unfortunately this is not working the way it should. When using the GroupSortingExpression I get an error saying "LINQ to Entities does not recognize the method ...ElementAt[...], and this method cannot be translated into a store expression."

What I have: a table containing SalesOrders with properties "OrderDate" and "SalesOrderNumber"; EF 6.1 as ORM using CodeFirst. I try to create a QCV with the following GroupDescriptor: 

1.var yearGroupDescriptor = new GroupDescriptor<SalesOrderHeader, int, string>
2.{
3.    GroupingExpression = s => s.OrderDate.Year,
4.    GroupSortingExpression = g => g.ElementAt(0).SalesOrderNumber,
5.    SortDirection = ListSortDirection.Descending
6.};
7.this.SalesOrderList.GroupDescriptors.Add(yearGroupDescriptor);

The GroupingExpression itself is working like a charm, but the GroupSortingExpression throws the error. 

What I like to achieve:

1. Group the list on year and monthname in descending order like this:
    March 2015

    
February 2015
    January 2015
    December 2014
    November 2014
    ...

2. Inside the group sort the data by SalesOrderNumber descending like that:
    February 2015
        124
        123
        122
        ...

Any idea?

TIA

Neils

0
Dimitrina
Telerik team
answered on 08 Apr 2015, 07:56 AM
Hello Neils,

It seems your bound data object does not support this method. You should build a valid LINQ expression and pass it as GroupSortingExpression based on the options your data suggest.
RadGridView's data engine performs all the data operations with LINQ and the way to modify the default behavior would be this.

Regards,
Dimitrina
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Heiko
Top achievements
Rank 1
answered on 08 Apr 2015, 10:17 AM

Hello Dimitrina,

LINQ comes in various forms. What I am talking about is "LINQ to Entities" which is the base of Entity Framework and it does not know the "ElementAt..." method.

Regards
Neils

0
Dimitrina
Telerik team
answered on 10 Apr 2015, 09:00 AM
Hello Neils,

As it turns out, RadGridView cannot interact on this. You can check the following threads on Stackoverflow instead:
LINQ to Entities does not recognize the method ElementAt(i);
LINQ to Entities does not recognize the method ElementAt[WebSecurity.CurrentUserId]
Getting the first result from a LINQ query - why does ElementAt<T>(0) fails when First<T>() succeeds?

LINQ to Entities does not recognize the method ElementAt[].

Hopefully, this helps.

Regards,
Dimitrina
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

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