My Setup
I have following (pseudo) EF classes:
class Department {
int ID,
string Name
}
class DepartmentCustomer {
int ID,
int CustomerID,
Customer Customer,
string Information,
int DepartmentID,
Department Department
}
Class Customer {
int ID,
string Name,
int? CityID,
City City
}
Class City{
int ID,
string Name,
string PostalCode
}
I have a form with a BindingSource for List(of DepartmentCustomer) and the following fields :
DepartmentCustomer.Informationas a (read-write) textboxDepartmentCustomer.Customer.Nameas a (readonly) textboxDepartmentCustomer.Customer.Cityas a (read-write) combobox.
The combobox for the city name has following properties:
ComboBoxCity.DataSource = ListOfCities ' = List(Of City)
ComboBoxCity.ValueMember = "Id"
ComboBoxCity.DisplayMember = "Name"
ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))
My Problem
When DepartmentCustomerBindingSource.CurrentItemChanged occurs, the Combobox is not synchronized; it sometimes updates to the correct value, but when navigating further through the BindingSource it keeps the previous item selected
So, I have to do the following to update the combobox manually
Private Sub DepartmentCustomerBindingSource_CurrentItemChanged(sender As Object, e As EventArgs) Handles DepartmentCustomerBindingSource.CurrentItemChanged
If DepartmentCustomerBindingSource.Current.Contact.City Is Nothing then
ComboBoxCity.SelectedIndex = -1
Else
ComboBoxCity.SelectedItem = DepartmentCustomerBindingSource.Current.Contact.City
End if
End Sub
(for the sake of simplicity I have ommitted casting in my code above. Also these example classes might not make sense IRL)
EDIT
Even the code above does not do what I want. For example: I have two instances of DepartmentCustomer, one with a Contact.City, the second without a Contact.City . When the form opens at the first instance, it shows the city; when I navigate to the second record, the combobox becomes empty, but when I go back to the first combobox, it stays empty; even more surprising, the first record has been updated to Contact.City = Nothing :'(
EDIT 2: My own not-preferred solution
I have removed the databinding from the combobox (ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))) and added the following sub
Private Sub ComboBoxCity_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBoxCity.SelectedValueChanged
DepartmentCustomerBindingSource.Current.Contact.City = ComboBoxCity.SelectedItem
End Sub
This works, but since I have quite a lot of these comboboxes in my form, I think there has to be an "automatic" way of synchronizing a two-way bound combobox...
Edit 3: I throw in the towel
Even my above "solution" is not working as expected; the Contact.City of the instances are not updated correctly when using the above code...
My Question
Why do I have to to this manually; Am I missing something? I would think that by setting the DataBinding, it would update the SelectedItem whenever the bound BindingSource is being navigated;
