R – Filtering combobox based on value member

comboboxdata-bindingwinforms

I have two list that I load when my app starts. The first one loads a complete set of data from the database, the second one independently loads a set of associated data from file.
Each list is loaded into a BindingSource and set as the DataSource for their respective combobox. The data is loading just fine.
The issue is that I need to have the second comboBox only display the elements of its list that correspond with the selected value of the first list.
I have attempted to set the value members to the referential bit of data, but cant figure out how to get the comboBoxSettings to only show the the items whose EventID matches the selected item's EventID from the EventList comboBox.

         //Event List comboBox
        comboBoxEventList.DataSource = _eventSimPresenter.BindingSourceEventList;
        comboBoxEventList.DisplayMember = "DisplayName";
        comboBoxSettings.ValueMember = "EventID";

        //Settings combobox
        comboBoxSettings.DataSource = _eventSimPresenter.BindingSourceUserSettings;
        if (_eventSimPresenter.BindingSourceUserSettings.Count > 0)
        {
            comboBoxSettings.DisplayMember = "EventName";
            comboBoxSettings.ValueMember = "EventID";
        }

thanks!

Best Answer

You can reate a method in _eventSimPresenter that return a BindingSourceUserSettings by eventId. When the 1st comboBox changes, take the selected eventId and update 2nd comboBox datasource:

    ...
    comboBoxSettings.DataSource =
              _eventSimPresenter.GetBindingSourceUserSettings(selectedEventId)
    if (_eventSimPresenter.BindingSourceUserSettings.Count > 0)
    {
        comboBoxSettings.DisplayMember = "EventName";
        comboBoxSettings.ValueMember = "EventID";
    }

In other terms, the filtering should be applied to the datasource since it's not possible to do it via the comboBox.

Related Topic