Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Items in WinForms ComboBox not updating when DataSource values change

I have a ComboBox bound to a List via a DataSource. For some reason, when the datasource items change, the items in the combo box don't seem to automatically update. I can see in the debugger the datasource contains the correct items.

There are lots of answers on StackOverflow about this, but most are either unanswered, don't work for me, or require changing from using Lists to BindingLists which I cannot do this instance due to the volume of code which uses methods BindingLists don't have.

Surely there must be a simple way of just telling the ComboBox to refresh it's items? I can't believe this doesn't exist. I already have an event which fires when the Combo needs to be updated, but my code to update the values has no effect.

Combo declaration:

    this.devicePortCombo.DataBindings.Add(
             new System.Windows.Forms.Binding("SelectedValue", 
               this.deviceManagementModelBindingSource, "SelectedDevice", true,
               DataSourceUpdateMode.OnPropertyChanged));
    this.devicePortCombo.DataSource = this.availableDevicesBindingSource;

Code to update the combobox:

private void Instance_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "AvailableDevices")
    {
        // Rebind dropdown when available device list changes.
        this.Invoke((MethodInvoker)delegate
        {
            devicePortCombo.DataSource = AvailableDevicesList;
            devicePortCombo.DataBindings[0].ReadValue();
            devicePortCombo.Refresh();
        });
    }
}
like image 735
NickG Avatar asked Oct 19 '22 04:10

NickG


1 Answers

You are not binding the DataGridview's DataSource to same BindingSource object in your case this.availableDevicesBindingSource which bound first time. but later you are binding to different object AvailableDevicesList. again you are using another binding source for SelectedValue i.e this.deviceManagementModelBindingSource.

use one BindingSource only, may solve your issue

like image 174
Kailash Chandra Polai Avatar answered Nov 02 '22 05:11

Kailash Chandra Polai