您可以制作自己的 SortableBindingList 类,如下所示:
Imports System.ComponentModel
Imports System.Reflection
Public Class SortableBindingList(Of T)
Inherits BindingList(Of T)
Private Property IsSorted As Boolean
Private Property SortDirection As ListSortDirection
Private Property SortProperty As PropertyDescriptor
Protected Overrides ReadOnly Property SupportsSortingCore() As Boolean
Get
Return True
End Get
End Property
Protected Overrides ReadOnly Property SortDirectionCore() As ListSortDirection
Get
Return _SortDirection
End Get
End Property
Protected Overloads Overrides ReadOnly Property SortPropertyCore() As PropertyDescriptor
Get
Return _SortProperty
End Get
End Property
Protected Overloads Overrides Sub ApplySortCore(ByVal PDsc As PropertyDescriptor, ByVal Direction As ListSortDirection)
Dim items As List(Of T) = TryCast(Me.Items, List(Of T))
If items Is Nothing Then
IsSorted = False
Else
Dim PCom As New PCompare(Of T)(PDsc.Name, Direction)
items.Sort(PCom)
IsSorted = True
SortDirection = Direction
SortProperty = PDsc
End If
OnListChanged(New ListChangedEventArgs(ListChangedType.Reset, -1))
End Sub
Protected Overloads Overrides ReadOnly Property IsSortedCore() As Boolean
Get
Return _IsSorted
End Get
End Property
Protected Overrides Sub RemoveSortCore()
_IsSorted = False
End Sub
#Region " Constructors "
Sub New(ByVal list As ICollection(Of T))
MyBase.New(CType(list, Global.System.Collections.Generic.IList(Of T)))
End Sub
Sub New()
MyBase.New()
End Sub
#End Region
#Region " Property comparer "
Private Class PCompare(Of T)
Implements IComparer(Of T)
Private Property PropInfo As PropertyInfo
Private Property SortDir As ListSortDirection
Friend Sub New(ByVal SortProperty As String, ByVal SortDirection As ListSortDirection)
SortProperty = CheckPropertyName(SortProperty)
PropInfo = GetType(T).GetProperty(SortProperty)
SortDir = SortDirection
End Sub
''' <summary>
''' Used to change the name of the sorted property for certain classes.
''' </summary>
''' <param name="SortProperty"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function CheckPropertyName(ByVal SortProperty As String) As String
If SortProperty = "BinNumber" AndAlso GetType(T).Name = "BinReportDisplay" Then
Return "BinNumberSort"
End If
Return SortProperty
End Function
Friend Function Compare(ByVal x As T, ByVal y As T) As Integer Implements IComparer(Of T).Compare
Return If(SortDir = ListSortDirection.Ascending, Comparer.[Default].Compare(PropInfo.GetValue(x, Nothing),
PropInfo.GetValue(y, Nothing)), Comparer.[Default].Compare(PropInfo.GetValue(y, Nothing),
PropInfo.GetValue(x, Nothing)))
End Function
End Class
#End Region
End Class
然后根据您希望的排序方式对列表进行排序。