What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleBind a collection of objects to a DataGrid and let it sort on the columns in VB .NET
DescriptionThis example shows how to bind a collection of objects to a DataGrid and let it sort on the columns in VB .NET.
KeywordsDataGrid, bind, data binding, collection, sort, IEditableObject, IBindingList
CategoriesVB.NET, Database, Controls
This is actually a bigger pain that you might think. The DataGrid will only sort objects that implement the IBindingList interface. This interface is a lot more complicated than ICompare.

First, the Employee class implements IEditableObject. This actually isn't too hard and is kind of interesting. It provides methods to start editing the object (saves a copy of the data), rollback the changes (restore the saved data), and accept the changes.

Public Class Employee
    Implements IEditableObject

    Private Structure EmployeeData
        Public FirstName As String
        Public LastName As String
    End Structure

    Private m_MyData As EmployeeData
    Private m_BackupData As EmployeeData
    Private m_Editing As Boolean
    Public Parent As EmployeeList

    Public Property FirstName() As String
            Return m_MyData.FirstName
        End Get
        Set(ByVal Value As String)
            m_MyData.FirstName = Value
        End Set
    End Property
    Public Property LastName() As String
            Return m_MyData.LastName
        End Get
        Set(ByVal Value As String)
            m_MyData.LastName = Value
        End Set
    End Property
    Public Sub New(ByVal first_name As String, ByVal _
        last_name As String)
        m_MyData.FirstName = first_name
        m_MyData.LastName = last_name
    End Sub
    Public Sub New()
        m_MyData.FirstName = "<Unknown>"
        m_MyData.LastName = "<Unknown>"
    End Sub

    Public Sub BeginEdit() Implements _
        If Not m_Editing Then
            m_BackupData = m_MyData
            m_Editing = True
        End If
    End Sub

    Public Sub CancelEdit() Implements _
        If m_Editing Then
            m_MyData = m_BackupData
            m_Editing = False
        End If
    End Sub

    Public Sub EndEdit() Implements _
        m_Editing = False
    End Sub

    Friend Property Parents() As EmployeeList
            Return Parent
        End Get
        Set(ByVal Value As EmployeeList)
            Parent = Value
        End Set
    End Property

    Private Sub OnCustomerChanged()
        If Not m_Editing And Not (Parent Is Nothing) Then
        End If
    End Sub 'OnCustomerChanged
End Class
The EmployeeComparer class is a fairly straightforward IComparer for Employee objects.
Public Class EmployeeComparer
    Implements IComparer

    Private m_SortOnProperty As String
    Public Sub New(ByVal sort_on_property As String)
        m_SortOnProperty = sort_on_property
    End Sub

    Public Function Compare(ByVal x As Object, ByVal y As _
        Object) As Integer Implements _
        Dim emp1 As Employee = DirectCast(x, Employee)
        Dim emp2 As Employee = DirectCast(y, Employee)
        Dim key1, key2 As String
        If m_SortOnProperty = "FirstName" Then
            key1 = emp1.FirstName & vbNullChar & _
            key2 = emp2.FirstName & vbNullChar & _
            key1 = emp1.LastName & vbNullChar & _
            key2 = emp2.LastName & vbNullChar & _
        End If
        Return String.Compare(key1, key2)
    End Function
End Class
The EmployeeList class is where things get ugly. This class inherits CollectionBase and implements IBindingList. It provides methods to add, remove, and otherwise manage a list of Employees. It also providesmostly routines for sorting and controlling the sort on the list. See the code for details.

I actually did not get all of this to work. The example program sorts on the column you click but only in ascending order. But it's clear I wont' have time to work more on this one. If you get the rest of it to work, let me know.

Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.