UWP FlexGrid Sorting Options

Posted by: uhlmandanl on 6 May 2022, 3:07 am EST

  • Posted 6 May 2022, 3:07 am 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?
  • Replied 8 May 2022, 8:20 pm 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
  • Replied 9 May 2022, 12: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
  • Replied 9 May 2022, 3:18 pm 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
  • Replied 10 May 2022, 11:32 pm EST

    Thanks Kartik! I appreciate the help.

    Dan
  • Replied 13 May 2022, 2:26 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

  • Replied 15 May 2022, 3:39 pm 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