How to group a column by C# code

6 posts, 1 answers
  1. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 24 Apr 2010 Link to this post

    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
  2. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 24 Apr 2010 Link to this post

    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 ?
  3. DevCraft banner
  4. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 25 Apr 2010 Link to this post

    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 ?
  5. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 25 Apr 2010 Link to this post

    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
  6. Answer
    Milan
    Admin
    Milan avatar
    1989 posts

    Posted 26 Apr 2010 Link to this post

    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.
  7. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 26 Apr 2010 Link to this post

    Oh great hint.  Thank you.  I didn't think about the Nullable ListSortDirection.
Back to Top
DevCraft banner