开发者

ComboBox doesn't automatically update when DataSource changes?

开发者 https://www.devze.com 2023-04-03 22:11 出处:网络
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:

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.Dat开发者_开发知识库aSource = 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.


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 :)


You need observable collections and IPropertyChange implementation:

ComboBox bound to a ObservableCollection does not update

0

精彩评论

暂无评论...
验证码 换一张
取 消