Question Binding combobox to generic list

spidergirl1979

New member
Joined
Jun 26, 2009
Messages
3
Programming Experience
3-5
I'm at a bit of a loss and been grappling with this for several hours now, although I'm sure the solution is simple in nature.

What I would like to do is bind a combobox to my own object which is as follows:
VB.NET:
Public Class Categories
        Public Sub New(ByVal myConn As SqlConnection)
            Dim myConnection As SqlConnection = myConn 
            myConnection.Open()
            Dim myCommand = New SqlCommand("Select [CategoryID], [Description] from CategoryLU ORDER BY Description", myConnection)
            Dim dr = myCommand.ExecuteReader

            If dr.HasRows Then
                While dr.Read()
                    Me.ID.Add(dr("CategoryID"))
                    Me.Description.Add(dr("Description"))
                    _Count += 1
                End While
            End If

            dr.Close()
        End Sub

        Dim _ID As New List(Of Integer)
        Public Property ID() As List(Of Integer)
            Get
                Return _ID
            End Get
            Set(ByVal value As List(Of Integer))
                _ID = value
            End Set
        End Property

        Dim _Description As New List(Of String)
        Public Property Description() As List(Of String)
            Get
                Return _Description
            End Get
            Set(ByVal value As List(Of String))
                _Description = value
            End Set
        End Property       
    End Class
(I have put breakpoints and I know this object has data.)

So on my form how do I bind my combobox to this object? The code I have is below:
VB.NET:
        Dim bs As New BindingSource
        bs.DataSource = New Inventory.Categories(myconn)
        cboCategory.DataSource = bs
        cboCategory.DisplayMember = "Description"
        cboCategory.ValueMember = "CategoryID"
but it returns the following error: Cannot bind to the new display member. Parameter name: newDisplayMember
 
You can bind the List itself to the datasource.

VB.NET:
Dim inv_cat As New Inventory.Categories(myconn)

cboCategory.DataSource = inv_cat.Description
 
Did I answer this question on another forum? Your Categories class is all wrong. You need to completely redesign it, breaking it into at least two separate classes and probably three.
 
Back
Top