This is a migrated thread and some comments may be shown as answers.

Define ColumnSortDescriptor with custom Comparer(T)

1 Answer 241 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 1
Jason asked on 30 Sep 2011, 10:19 PM
In my application, I have a column in my RadGridView that is bound to a property of type 'System.Type'. When attempting to sort by this column, I get a System.ArgumentException{"At least one object must implement IComparable."}. I am not interested in wrapping the System.Type class at all to implement the IComparable(T) interface and would like to be able to provide a function delegate to the ColumnSortDescriptor to sort the types by name instead of using the default comparer as I could with a normal list.

Function CompareTypeByName(ByVal x As System.Type, ByVal y As System.Type) As Integer
  If x Is Nothing Then
    If y Is Nothing Then
      Return 0
    Else
      Return -1
    End If
  Else
    If y Is Nothing Then
      Return 1
    Else
      Return x.Name.CompareTo(y.Name)
    End If
  End If
End Function

Dim typeList As New List(Of System.Type) 
typeList.Add(GetType(String)) 
typeList.Add(GetType(Int32)) 
typeList.Sort(AddressOf CompareTypeByName)

Is there a way to do this (or something like it) currently?

Thanks,
Jason

1 Answer, 1 is accepted

Sort by
0
Jason
Top achievements
Rank 1
answered on 04 Oct 2011, 06:51 PM
I ended up resolving my problem with the following code when handling the table's Sorting event if anyone runs across the same issue. It still doesn't let me provide my own sorting delegate but it does accomplish what I was trying to do it.

Dim descriptor as New SortDescriptor(Of ObjectWithSystemType, String)() With { _ 
  .SortingExpression = Function(t) t.SystemType.Name, 
  .SortDirection = ListSortDirection.Ascending 
}
Tags
GridView
Asked by
Jason
Top achievements
Rank 1
Answers by
Jason
Top achievements
Rank 1
Share this question
or