I’m attempting to bind a combo to a collection of objects:
Dim t As New TradeOrderStatus()
Dim ts As List(Of TradeOrderStatus) = t.GetStatuses
With Me.cboTradeStatus
.DataSource = ts
.SelectedItem = Nothing
End With
This works fine and I see the list of items in the combo. However when I try to set the SelectedItem of the combo to one of the items:
Me.cboTradeStatus.SelectedItem = Trade.TradeStatus
nothing happens. Trade.TradeStatus is an instance of the class TradeOrderStatus and all the necessart statuses are visible in the drop-down list. The SelectedItem remains as Nothing (or as the first item in the list if I omit the .SelectedItem = Nothing line from the binding code).
Debug.Print(CStr(Me.cboTradeStatus.Items.Contains(.TradeStatus)))
also returns false. Can anyone help?
You mention it is a class; is it the same instance as one of those in the data-bound list? It needs to find an equality match. Alternatively, you can override
Equals(andGetHashCode()– always keep the two in sync) to achieve the same thing.(edit)
The simplest way to fix it is to bind to
SelectedValue; with the “full” example (below), something like:(edit)
Here’s a C# example (sorry, my VB-fu is weak) of providing custom equality of the different statuses – note the “uncomment to fix”: