Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ComboBox doesn't automatically update when DataSource changes?

For some reason when adding or remove items from the DataSource (a simple BindingList) the ComboBox updates accordingly but if I edit an item like this, it doesn't update automatically:

myBindingList[index].Name = "NewName";
myBindingList[index].Value = newValue;

In order to get it to update when I edit an item as opposed to creating or removing an item I have to do this after the change is made:

myComboBox.DataSource = null;
myComboBox.DataSource = myBindingList;

This fixes the problem but it seems like a rather messy solution. Also with large lists it may become slow (premature optimization I know) but still is there a way to force the ComboBox to update without completely re-assigning its DataSource?

Thanks for reading.

like image 627
Kyle McClooney Avatar asked Sep 11 '11 17:09

Kyle McClooney


People also ask

How do I refresh my ComboBox?

To do this, you will need to call the "Requery" method for the combo box. In this example, you have a combo box called Category. The Requery method forces the combo box to refresh the values that it lists.

Is the default event of ComboBox control?

By default, DropDownStyle property of a Combobox is DropDown. In this case user can enter values to combobox. When you change the DropDownStyle property to DropDownList, the Combobox will become read only and user can not enter values to combobox.


1 Answers

this is stated in the MSDN forums:

The IBindingList interface contains the ListChanged event where controls like the combobox hook up into if the underlying datasource assigned to it implements the said interface. your datasource must raise the corresponding ListChanged with proper ListChangeEventArgs if ever you add, remove, change, etc. your IBindingList implementor. this way, whenever the underlying source you used to bind to your combobox is changed, the corresponding UI control (combobox) is refreshed.

you say you are using BindingList and in fact you do get the combobox to reflect add or remove items events. I think you should do the update of the items already inside your BindingList in another way because looks like the proper BindingList events are not firing.

you could either investigate into that or simply live with reset and reassign the DataSource, I don't think is too bad, you are in Statefull Windows Forms application not in SatetLess Webforms so you do have your objects there all the time :)

like image 162
Davide Piras Avatar answered Sep 19 '22 18:09

Davide Piras