views:

261

answers:

2

I have this:

cmbConnections.DisplayMember = "Name";
cmbConnections.ValueMember = "Index";
cmbConnections.DataSource = DBConnectionSettings.ConnectionList;

All Ok!

Now add new item to list:

DBConnectionSettings.Connection c = new DBConnectionSettings.Connection();
c.Name = reply;
c.Index = DBConnectionSettings.ConnectionList.Count + 1;
DBConnectionSettings.ConnectionList.Add(c);

I now want to show this new item in the comboxbox as the selected item. Setting the text of the combobox fails.

Thanks

+1  A: 

Try putting a BindingSource between your DataSource and the ComboBox. After you've added your new item, call myBindingSource.ResetBindings(false); and then you will either set the Position property of myBindingSource or the SelectedValue property of the ComboBox.

Chris Porter
A: 

You have set the ValueMember of your combobox to "Index", which means you can select a value in the combobox by setting ComboBox.SelectedValue to the index of your item. I.e.:

cmbConnections.SelectedValue = c.Index;
Jakob Christensen
That throws an exception, cannot set SelectedValue to 0
Jon