I am having virtually the same problem as this:
C# Update combobox bound to generic list
However, I am trying to change the displayed strings; not add, remove, or sort. I have tried the BindingList solution provided in the referenced question, but it has not helped. I can see the combobox’s DataSource property is correctly updated as I edit the items, but the contents displayed in the combobox are not those in the DataSource property.
my code looks as follows:
mSearchComboData = new List<SearchData>(); mSearchComboData.Add(new SearchData('', StringTable.PatientID)); mSearchComboData.Add(new SearchData('', StringTable.LastName)); mSearchComboData.Add(new SearchData('', StringTable.LastPhysician)); mSearchComboData.Add(new SearchData('', StringTable.LastExamDate)); mBindingList = new BindingList<SearchData>(mSearchComboData); SearchComboBox.Items.Clear(); SearchComboBox.DataSource = mBindingList; SearchComboBox.ValueMember = 'Value'; SearchComboBox.DisplayMember = 'Display'; ...
When I try to update the content I do the following:
int idx = SearchComboBox.SelectedIndex; mBindingList[idx].Display = value; SearchComboBox.Refresh();
EDIT::
RefreshItems seems to be a private method. I just get the error message:
”System.Windows.Forms.ListControl.RefreshItems()’ is inaccessible due to its protection level’
ResetBindings has no effect.
If you were to change the entire object, meaning your entire SearchData object, then the bindinglist would have knowledge of this change, and therefore the correct events would internaly get fired, and the combobox would update. HOWEVER, since you’re only updating one property, the bindinglist has no idea that something has changed.
What you need to do is have your SearchData class implement INotifyPropertyChanged. Here’s a quick sample I wrote to demonstrate:
And here’s some code to test:
Since my class now implements INotifyPropertyChanged, the binding list gets ‘notified’ when something changes, and all this will thus work.