Question About Programmatically Sort Collumns

Posted by: oric-cq on 9 September 2017, 8:31 am EST

  • Posted 9 September 2017, 8:31 am EST

    Hi !

    For sort grid's columns, I'm using the following code:

    Private Sub SortCategory_Execute()
    '
    Dispatchers.Main.BeginInvoke(Sub()
    Dim categorySortDescription = _grid.CollectionView.SortDescriptions.Where(Function(n) n.PropertyName = "Category").FirstOrDefault

    If String.IsNullOrEmpty(categorySortDescription.PropertyName) Then
    _grid.CollectionView.SortDescriptions.Add(New SortDescription("Category", ListSortDirection.Ascending))
    Else
    Dim direction As ListSortDirection
    If categorySortDescription.Direction = ListSortDirection.Ascending Then
    direction = ListSortDirection.Descending
    Else
    direction = ListSortDirection.Ascending
    End If
    '
    Dim sd = _grid.CollectionView.SortDescriptions.Where(Function(n) n.PropertyName = "Category").FirstOrDefault
    _grid.CollectionView.SortDescriptions.Remove(sd)
    _grid.CollectionView.SortDescriptions.Add(New SortDescription("Category", direction))
    End If
    End Sub)
    End Sub


    My question is: Is my approach a good practice or there is an easier way?

    Best regards,
  • Replied 9 September 2017, 8:31 am EST

    Hello Ciro,

    If you wish to handle the Sorting manually then this is the suggested approach. That is, to get the underlying CollectionView the grid is bound to and handle the SortDescriptions Collection as per requirement.

    Regards,
    Reema
  • Marked as Answer

    Replied 9 September 2017, 8:31 am EST

    Thank you, Reema !

    Solved!

    Best regards,
Need extra support?

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

Learn More

Forum Channels