I have a dropdown list containing the days of the week - Monday to Sunday. It is populated with a user defined type of two values that map the numeric day of the week to it's name.
Public Structure WeekDays
Public ID As Integer
Public Text As String
Public Overrides Function ToString() As String
Return Me.Text
End Function
End Structure
The object I want to Bind to has an integer property DayOfWeek, and I want to bind the ID value of the selected item in the dropdown to the DayOfWeek property on the Object. eg. The user selects Thursday, and the ID of 4 is passed to the object.
In code I can retrieve the UDT of the SelectedItem, but I can't work out which property on the combo box to bind to.
- If I add the UDTs directly to the Items collection of the dropdown, the SelectedValue is Nothing.
- If I add the UDTs to a List(Of UDT) collection and set that as the dropdown's datasource, with the ValueMember set to ID and DisplayMember set to Text, the SelectedValue returns the whole UDT, not the ID as instructed in the ValueMember property.
Databinding seems to work really well for plain textboxes, but it seems to get way more pernickety when dealing with more complex controls.
Update: What I am looking for is the Binding statement. eg. Neither...
oB = New Binding("SelectedItem", Payroll, "DayOfWeek")
oB = New Binding("SelectedItem.ID", Payroll, "DayOfWeek")
... works. The first is just ignored (possibly because the SelectedItem property is Nothing), and the Second fails with a "Cannot bind..." error.