UWP FlexGrid Sorting Options

Posted by: uhlmandanl on 6 May 2022, 12:07 pm EST

    • Post Options:
    • Link

    Posted 6 May 2022, 12:07 pm EST

    We currently use the built in column sorting within FlexGrid. We now have a requirement to allow column sorting but leave certain records at the top no matter where they would fall in the sort order.

    Id Name

    1 Dan

    2 Joe

    3 Fred

    If I have the data above and Id 1 is the record I want to keep at the top, I would like to see:

    Id Name

    1 Dan

    3 Fred

    2 Joe

    Is it possible to do this?

  • Posted 9 May 2022, 5:20 am EST

    Hi Dan,

    In order to implement this requirement you need to create a custom comparer and set it to FlexGrid.CollectionView’s CustomSort property as follows:

    
    // ascending sort
    var collectionView = flexGrid.CollectionView as C1CollectionView;
    collectionView.CustomSort = new CustomSort() { SortDirection = ListSortDirection.Ascending };
    
    // descending sort
    var collectionView = flexGrid.CollectionView as C1CollectionView;
    collectionView.CustomSort = new CustomSort() { SortDirection = ListSortDirection.Descending };
    
    

    Please refer to the attached sample for CustomSort implementation. (see FlexGridSort.zip)

    Note: CustomSort present in the attached sample only sorts based on the Id property. You can customize it as per your requirements.

    Best Regards,

    Kartik

    FlexGridSort.zip

  • Posted 9 May 2022, 9:10 am EST

    Thanks Kartik. That’s sort of what I was looking. I’m more asking that if I use the native sorting (clicking on the column headers) is there a way to provide a custom comparator? Or would the better option be to supply the HeaderTemple with a button and use the click event to perform the sort?

    Thanks,

    Dan

  • Posted 10 May 2022, 12:18 am EST

    Hi Dan,

    It is not possible to use the CustomSort with default column sorting. So, as mentioned in the response you need to use your own HeaderTemplate and use the click handler to perform sorting.

    Best Regards,

    Kartik

  • Posted 11 May 2022, 8:32 am EST

    Thanks Kartik! I appreciate the help.

    Dan

  • Posted 13 May 2022, 11:26 am EST - Updated 30 September 2022, 9:06 am EST

    HI Kartik,

    We we’re using an ObservableCollection for our ItemsSource. I switched that over to a C1CollectionView for sorting. Once I did that, I started receiving an exception whenever I would edit a cell. This did not happen with the ObservableCollection. One of the columns uses the ColumnValueConverter and the others use TextBox, C1MaskedTextBox, or similar. I’ve attached an image of the stack trace. Any column we choose to edit receives the exception.

    Are there any things I need to do to switch to the C1CollectionView?

    Thanks,

    Dan

  • Posted 16 May 2022, 12:39 am EST

    Hi Dan,

    Thank you for sharing the stack trace.

    This issue generally occurs when your ItemSource is read-only. Could you please make sure that the data source you are passing to C1CollectionView is not read-only?

    JFYI, I didn’t get this exception while using List/ObervableCollection with C1CollectionView as follows:

    
    flexGrid.ItemsSource = new C1CollectionView(new List<User>()
    {
                    new User(){ Id = 1, Name = "Dan", Role = "Admin" },
                    new User(){ Id = 2, Name = "Joe", Role = "User" },
                    new User(){ Id = 3, Name = "Fred", Role = "User" }
    });
    
    

    In case you still face this issue then I would request you to share a small sample replicating this behavior.

    Additionally, I would also request you to create a new ticket for a new issue because it will help us in managing your tickets properly.

    Best Regards,

    Kartik

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels