I'm looking for a way to get the GridViewComboBoxColumn to sort on the DisplayMemberPath, but I do not want to code this for every single grid with custom sorting or every single GridViewComboBoxColumn I add to our application. There are a number of developers on this project and undoubtedly one will forget to do it, and this is not really maintainable either.
Ideally I could have my own SortableGridViewComboBoxColumn I could use that inherited from the GridViewComboBoxColumn but I have not had much luck going down this road so far. Is there any advice you can give or samples I can look at? I've been through almost everything on this forum already.
Thanks.
8 Answers, 1 is accepted
Actually such a feature has already been implemented. You may find additional information in this forum thread. Please let me know in case you have any further questions.
All the best,Didie
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
What happens if I add a new object to the collection? Now I have to loop through the entire collection and set the "SortID" property for every combo box column in the grid. Not elegant.
I am looking for a way to get the control to sort by DisplayMemberPath so I don't need to create many many seperate properties for the grids in our application.
Indeed additional setting of the SortMemberPath will be needed. In order to avoid this, you could handle the Sorting event of the RadGridView and apply your custom sorting which sorts based on the DisplayMemberPath. You can check the CustomSorting WPF Demo and our online documentation for an example.
Greetings,Didie
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
Is there no way to inherit from the GridViewComboBoxColumn and write some generic code to sort? That way we simply use this new column and the application will behave as all users would expect it to behave.
Unfortunately this is what we can suggest regarding Sorting of this column. Inheriting it will not help as the sorting logic is in the Data Engine, it is not in the column.
Kind regards,Didie
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
No, I never got it working. I did try using a custom column but that had its own challenges. What I typically do now is use a regular data column and change the cell edit template to a combo box.
I found out a solution, maybe it could be simplier, but for me it is good enough.
First you have to set ".IsCustomSortingEnabled = True" on the GridViewComboBoxColumn.
Then in the sorting handler:
Private Sub Datagr_Sorting(sender As Object, e As GridViewSortingEventArgs) Handles Datagr.Sorting Dim CB = TryCast(e.Column, GridViewComboBoxColumn) If Not CB Is Nothing Then If e.Column.IsCustomSortingEnabled Then Dim cbis = TryCast(CB.ItemsSource, DataView) If Not cbis Is Nothing Then Datagr.SortDescriptors.Clear() If e.OldSortingState = SortingState.None Or e.OldSortingState = SortingState.Descending Then e.NewSortingState = SortingState.Ascending ElseIf e.OldSortingState = SortingState.Ascending Then e.NewSortingState = SortingState.Descending 'Else ' 'If the sorting state is descending, apply default sorting to the items. ' e.NewSortingState = SortingState.None End If ' create new DV Dim dgvDV = DGVNaturalColumnSort(CB.SelectedValueMemberPath, e.NewSortingState, CB.DisplayMemberPath, cbis) 'Set the sorted collection as source of the RadGridView e.DataControl.ItemsSource = dgvDV e.Cancel = True Exit Sub End If End If End If 'All others If e.NewSortingState = SortingState.None Then e.NewSortingState = SortingState.Ascending End IfEnd sub
and
Private Function DGVNaturalColumnSort(colName As String, sortt As SortingState, sortName As String, dbis As DataView) As DataView Dim NComparer As New NaturalStringComparer(sortt, colName, sortName, dbis) Try Dim tempDT = datatable.DefaultView.Table.AsEnumerable().OrderBy(Function(s) s.Field(Of Object)(colName), NComparer).CopyToDataTable Return New DataView(tempDT) Catch ex As Exception Return Nothing End Try End Function
and
Public Class NaturalStringComparer Implements IComparer(Of String) Dim mysortflipper As SortingState Dim mycolName As String Dim mysortName As String Dim dict As New Dictionary(Of Integer, String)() Public Sub New() End Sub Public Sub New(sort As SortingState, colName As String, sortName As String, dbis As DataView) mysortflipper = sort mycolName = colName mysortName = sortName For Each a In dbis.Table.AsEnumerable dict.Add(a.Field(Of Object)(colName), a.Field(Of Object)(sortName)) Next End Sub Public Function Compare(x As String, y As String) As Integer _ Implements IComparer(Of String).Compare ' convert DBNull to empty string Dim x1 = If(String.IsNullOrEmpty(x), String.Empty, dict(x)) Dim y1 = If(String.IsNullOrEmpty(y), String.Empty, dict(y)) 'String Compare Select Case mysortflipper Case SortingState.Ascending Return String.Compare(x1, y1) Case Else Return String.Compare(y1, x1) End Select End Function End Class
Maybe you will find an eleganter solution.
Regards,
Patrick