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

How to group a column by C# code

5 Answers 127 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Marc Roussel
Top achievements
Rank 2
Marc Roussel asked on 24 Apr 2010, 11:42 AM
Hi,

As we can do when we drag a column in the GroupPanel,  I want to do it by C# code to make a group column by default since my columns are added dynamicly by code.

How do you do that ?
I don'T see something like GroupBy or anything that let me group a column.
Where do I go,  I tried looking at your demos and nothing there show me how to do it by C# code

Thank you

5 Answers, 1 is accepted

Sort by
0
Marc Roussel
Top achievements
Rank 2
answered on 24 Apr 2010, 12:04 PM
I succeeded with this but Sorting is a mystery.

rgvPhones.GroupDescriptors.Add(new Telerik.Windows.Data.GroupDescriptor() { DisplayContent = "Department", Member = "Department" });   
 
 

When I try to sort it askes for a System.ComponentModel.ListSortDirection which I cannot figure out how to use
I mean usually when you use SortDirection = after the = the intellisence should kicks in to let you choose the sorting direction no ?
0
Marc Roussel
Top achievements
Rank 2
answered on 25 Apr 2010, 11:32 AM
Ok so

SortDirection = ListSortDirection.Ascending 

this works except that SortDirection have no intellisence kicking in and ListSortDirection is in System.ComponentModel
Why it isn't internal to Telerik controls ?
0
Marc Roussel
Top achievements
Rank 2
answered on 25 Apr 2010, 11:48 AM

I have a method that creates columns in the grid and since the SortDirection is weird, I have to do this in my code because the SortDirection doesn't match the sorting of a column.

if(IsGroupped)  
    if(SortingState != Telerik.Windows.Controls.SortingState.None)  
        if(SortingState == Telerik.Windows.Controls.SortingState.Ascending)  
            GridView.GroupDescriptors.Add(new GroupDescriptor() { DisplayContent = BindingPath, Member = BindingPath, SortDirection = System.ComponentModel.ListSortDirection.Ascending });  
        else 
            GridView.GroupDescriptors.Add(new GroupDescriptor() { DisplayContent = BindingPath, Member = BindingPath, SortDirection = System.ComponentModel.ListSortDirection.Descending });  
    else 
        GridView.GroupDescriptors.Add(new GroupDescriptor() { DisplayContent = BindingPath, Member = BindingPath });  
 

And it's even more complicated since ListSortDirection doesn't even have a None so using it as parameters forces us to have overloads
0
Accepted
Milan
Telerik team
answered on 26 Apr 2010, 08:37 AM
Hi Marc Roussel,

Sorting and grouping should be as straightforward as :

01.this.myGrid.SortDescriptors.Add(
02.    new Telerik.Windows.Data.SortDescriptor() { 
03.        Member = "Name", SortDirection = 
04.        System.ComponentModel.ListSortDirection.Ascending });
05.  
06.this.myGrid.GroupDescriptors.Add(
07.    new Telerik.Windows.Data.GroupDescriptor() { 
08.        Member = "Name"
09.        SortDirection = System.ComponentModel.ListSortDirection.Ascending });

Still, your scenario poses a bit of a problem but you can easily write a function that converts SortDirection to ListSortDirection and use it in your application:

01.private ListSortDirection? ToSortDirection(SortingState state)
02.{
03.  
04.    switch (state)
05.    {
06.        case SortingState.Ascending :
07.            return ListSortDirection.Ascending;
08.  
09.        case SortingState.Descending :
10.            return ListSortDirection.Descending;
11.  
12.        default:
13.            return null;
14.    }
15.}


Kind regards,
Milan
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Marc Roussel
Top achievements
Rank 2
answered on 26 Apr 2010, 01:27 PM
Oh great hint.  Thank you.  I didn't think about the Nullable ListSortDirection.
Tags
GridView
Asked by
Marc Roussel
Top achievements
Rank 2
Answers by
Marc Roussel
Top achievements
Rank 2
Milan
Telerik team
Share this question
or