DataGridView, Custom Buisness Object, And Sorting

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 :)
 
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.

Nevermind... My needs were simple, so I just changed all the columns to
Programmatic and am handling the sorting myself :)
 
T

Tom Shelton

Tom, maybe you need to override/implement the SortDirectionCore property of
BindingList? It always returns Ascending by default.

hmmm... maybe. I'll look at that. Thanks.
 
T

Tom Shelton

Tom, maybe you need to override/implement the SortDirectionCore property of
BindingList? It always returns Ascending by default.

Steve! You are the Man! That was it. A couple of lines of code, and
bling - it works.

Thanks. I'm going to have to read those docs a tad closer next time :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top