T
Tom Shelton
Ok... I haven't really used the DataGridView before - I've always used
a third party grid, but I now have a project were I have to use this
particular control. I've been searching the documentation, the web, etc
- and as near as I can tell I'm doing everything correctly - but, I can
not get the sorting working correctly.
So, here's the deal. I have a simple buisness object that is simply a
container for some data. It looks something like:
<Serializable()> _
Class ConnectionEntry
Public Property Database As String
Get
Return _dataBase
End Get
Set (ByVal value As String)
_dataBase = value
End Set
End Property
Public Property LastConnect As Date
Get
Return _lastConnect
End Get
Set (ByVal value As Date)
_lastConnect = value
End Set
End Property
End Class
Anyway, that's not the exact class, it has a handful of other
properties, all simple types - String, Boolean, etc.
Anyway, I actually serialize and deserialize a list of these objects and
save it in the applications user settings. On startup, I have a dialog
that displays this list to the user. That list is displayed in a
DataGridView. The user can't edit the individual entries - they simply
select them and I wanted to let them sort the entries in the list based
on any of the properties....
So, because this is a plain old object, I followed the various
instructiosn found on the web, and ended up creating a
SortableBindingList(Of T) class to use as the data source of the
DataGridView. It is pretty simple, It looks pretty much like:
Friend Class SortableBindingList(Of T)
Inherits BindingList(Of T)
Public Sub New ()
End Sub
Public Sub New (ICollection<T> list)
For Each item As T In list
Me.Add (item)
Next
End Sub
Protected Override ReadOnly Property SupportsSortingCore
Get
Return True
End Get
End Sub
Protected Override Sub ApplySortCore ( _
ByVal prop As PropertyDescriptor, _
ByVal direction As ListSortDirection)
Dim cItems As List(Of T) = TryCast (Me.Items, List(Of T))
If Not cItems Is Nothing Then
Dim gc As New GenericComparer(Of T)(prop, direction)
cItems.Sort(gc)
OnListChanged ( _
New ListChangedEventArgs (ListChangedType.Reset, prop))
End If
End Sub
End Class
GenericComparer is simply a class that implements IComparer(Of T) - it's
pretty straight forward and it works. I can call Sort on the grid
programmatically, and it sorts correctly. Both Ascending and
Descending.
So, what is the problem? Well, there are two actually
1) The columns do not show the SortDirectionGlyph - even though the
SortMode for all columns is Automatic.
2) And here is the big one - my ApplySortCore method is always being
called with a direction of Ascending. In other words, it doesn't
toggle, Ascending to Descending.
So, is that normal? Am I doing something wrong? Do I need to do more?
Do, I need to manually toggle the sorting order somehow?
Thanks
a third party grid, but I now have a project were I have to use this
particular control. I've been searching the documentation, the web, etc
- and as near as I can tell I'm doing everything correctly - but, I can
not get the sorting working correctly.
So, here's the deal. I have a simple buisness object that is simply a
container for some data. It looks something like:
<Serializable()> _
Class ConnectionEntry
Public Property Database As String
Get
Return _dataBase
End Get
Set (ByVal value As String)
_dataBase = value
End Set
End Property
Public Property LastConnect As Date
Get
Return _lastConnect
End Get
Set (ByVal value As Date)
_lastConnect = value
End Set
End Property
End Class
Anyway, that's not the exact class, it has a handful of other
properties, all simple types - String, Boolean, etc.
Anyway, I actually serialize and deserialize a list of these objects and
save it in the applications user settings. On startup, I have a dialog
that displays this list to the user. That list is displayed in a
DataGridView. The user can't edit the individual entries - they simply
select them and I wanted to let them sort the entries in the list based
on any of the properties....
So, because this is a plain old object, I followed the various
instructiosn found on the web, and ended up creating a
SortableBindingList(Of T) class to use as the data source of the
DataGridView. It is pretty simple, It looks pretty much like:
Friend Class SortableBindingList(Of T)
Inherits BindingList(Of T)
Public Sub New ()
End Sub
Public Sub New (ICollection<T> list)
For Each item As T In list
Me.Add (item)
Next
End Sub
Protected Override ReadOnly Property SupportsSortingCore
Get
Return True
End Get
End Sub
Protected Override Sub ApplySortCore ( _
ByVal prop As PropertyDescriptor, _
ByVal direction As ListSortDirection)
Dim cItems As List(Of T) = TryCast (Me.Items, List(Of T))
If Not cItems Is Nothing Then
Dim gc As New GenericComparer(Of T)(prop, direction)
cItems.Sort(gc)
OnListChanged ( _
New ListChangedEventArgs (ListChangedType.Reset, prop))
End If
End Sub
End Class
GenericComparer is simply a class that implements IComparer(Of T) - it's
pretty straight forward and it works. I can call Sort on the grid
programmatically, and it sorts correctly. Both Ascending and
Descending.
So, what is the problem? Well, there are two actually
1) The columns do not show the SortDirectionGlyph - even though the
SortMode for all columns is Automatic.
2) And here is the big one - my ApplySortCore method is always being
called with a direction of Ascending. In other words, it doesn't
toggle, Ascending to Descending.
So, is that normal? Am I doing something wrong? Do I need to do more?
Do, I need to manually toggle the sorting order somehow?
Thanks