ComboBox with datasource = ArrayList property of an object not displaying newly added item's text.
Posted on 2006-05-14
I've got a class "Customer" with a public property called "Hosts", which is an ArrayList of a class called "Host" which has 2 properties ("HostID" and "Name").
In Form_Load, after populating the objects Customer and Hosts from a database, the combobox propertly shows the list of Hosts by the foloowing code
Ptivate Sub LoadHostCombo()
Me.comboHost.DataSource = Nothing
Me.comboHost.DataSource = AppSession.CurrentCustomer.Hosts
Me.comboHost.DisplayMember = "Name"
Me.comboHost.ValueMember = "HostID"
I then set the SelectedIndex to 0 and all is groovy (i.e. the comboBox displays the propery text, my SelectedIndexChanged event fires (it populates the form with the selected Hosts's data).
I have a LinkLabel captioned New"that prompts the user to enter a new Host Name, adds it to the Hosts collection of CurrentCustomer, and then calls LoadHostCombo to refresh the DataSource for the combobox. I then attempt to set the SelectedIndex property of the combobox to select the newly added item, which in turn fires the SelectedIndexChanged event that displays the Hosts's data on the form.
Everything works fine except that the text in the combobox doesn't change, i.e. I see the new Hosts data on the form where expected, but the text in the combo displays the originally selected Host's Name. From that point on, even if I manually select another preexisting Host in the combo, I see the proper data on my form, but the combobox refuses to display the selected items text.
What am I missing? Oh yeah, it get's totally screwy if i break in debug, because when i come back to the form, the combobox sometimes is just white-space (no dropdown arrow or nothing), and sometimes it comes back with a combobox sized piece of the debug window inplace of the control. I can click on the right side and it functions like a combo box, but it's got busshit in the visuals...