I use a class that looks something like this:
Public Class ComboboxBinder(Of TKey, TValue)
Inherits List(Of KeyValuePair(Of TKey, TValue))
Public Sub New()
End Sub
Public Overloads Sub Add(ByVal key As TKey, ByVal value As TValue)
MyBase.Add(New KeyValuePair(Of TKey, TValue)(key, value))
End Sub
Public Sub Bind(ByVal control As ComboBox)
control.DisplayMember = "Value"
control.ValueMember = "Key"
control.DataSource = Me
End Sub
End Class
then to use it you would put something like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim binder As New ComboboxBinder(Of Guid, String)
binder.Add(Guid.NewGuid, "Item 1")
binder.Add(Guid.NewGuid, "Item 2")
binder.Add(Guid.NewGuid, "Item 3")
binder.Add(Guid.NewGuid, "Item 4")
binder.Bind(ComboBox1)
End Sub
Winforms is such a pain with binding, but this solution works well enough for our company.
Also worth noting is that you cannot bind to a Dictionary
, it must be an IList
or and IListSource
, hence why the Combobox
binder is a List<KeyValuePair<TKey,TValue>>