Click here to Skip to main content
15,898,936 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello:

I understand how to bind data to a ComboBox programmatically:

For Example:

VB
ComboBox1.DataSource = data.EmployeeList()
ComboBox1.DisplayMember = "Name"
ComboBox1.ValueMember = "EmployeeID"


However, what I am attempting to do is bind data using the designer and a Binding Source Object.

As demonstrated below, using the designer I added a data source and configured the display and value member but there is no data when the form loads.

How do I bind a list using the designer to a Combobox?

What I have tried:

First I created a Data Source:

VB
Public Class Employee

    Public Property EmployeeID As String
    Public Property Name As String

End Class


VB
Public Class Data

    Public Function Data() As List(Of Employee)

        Return New List(Of Employee)() From { _
         New Employee() With { _
          .Name = "George Benson", _
          .EmployeeID = 321
         }, _
         New Employee() With { _
          .Name = "Gladys Knight", _
          .EmployeeID = 322
         }, _
         New Employee() With { _
          .Name = "David Bowie", _
          .EmployeeID = 323
         }, _
         New Employee() With { _
          .Name = "Steve Wonder", _
          .EmployeeID = 324
         }, _
         New Employee() With { _
          .Name = "Tina Turner", _
          .EmployeeID = 325
         }
        }
    End Function

End Class



Next I created a View Model with a single read only property so that I can bind the
ComboBox to it.

Note: this is not full implementation of MVP Or MVPM, this is for demonstration only.

VB
Public Class EmployeeViewModel

    Private _listEmployees As Data

    Sub New()
        _listEmployees = New Data()
    End Sub

    Public ReadOnly Property EmployeeList() As List(Of Employee)
        Get
            Return _listEmployees.Data()
        End Get
    End Property

End Class
Posted
Updated 12-Sep-17 7:48am

I have a sample project in an article that does what you want with a ListBox, but the ComboBox is just the same: Working with JSON in C# & VB[^] Download the code (v1.3) and look at the VB version of WinFormSimpleCollection project. This is done as a MVVM-esque app. It includes this little helper function for binding the list of models to the control:
VB
Public Shared Sub BindList(Of TModel)(ctrl As ListControl, dataSource As IList(Of TModel), displayMember As String, valueMember As String)

    ctrl.DisplayMember = displayMember
    ctrl.ValueMember = valueMember
    ctrl.DataSource = dataSource

End Sub

Update: this may also be helpful: c# - WinForms. Design-time Data binding to child control's property - Stack Overflow[^]
 
Share this answer
 
v2
Comments
Member 13404587 12-Sep-17 14:32pm    
I really like the idea of using a generic type and passing parameters but I am not certain how this will work if I have multiple models.
Member 13404587 12-Sep-17 14:32pm    
I really like the idea of using a generic type and passing parameters but I am not certain how this will work if I have multiple models.
Graeme_Grant 12-Sep-17 18:53pm    
As I mentioned, download the code and try it out. :)
Member 13404587 12-Sep-17 14:32pm    
I really like the idea of using a generic type and passing parameters but I am not certain how this will work if I have multiple models.
Here is an excellent article describing several ways to bind data: A Detailed Data Binding Tutorial[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900