Click here to Skip to main content
15,168,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Sorry in advance for the long question ...

I'm using C# winforms on my app.

I have 2 combo boxes (id and type) and 1 textbox (name) that are bind to a data
that represents an item in a list

Changing the 1st combo box (id) - changes the second (type) and the textbox (name),
but changing the second combo box (type) causes the 1st (id) and the textBox (name)
to change too.

Also, the 2nd combo box (type) doesn't show all the enum entries.

I've declared the following enum and class

enum EnumItemType

class Item : INotifyPropertyChanged

//type property 
  private EnumItemType m_eType;
  public EnumItemType TYPE
    get => m_eType;
      m_eType= value;

//id property 
  private int m_iId;
  public int ID
    get => m_iId;
      m_iId = value;

//name property 
  private string m_sName;
  public string NAME
    get => m_sName;
      m_sName= value;

  public Item(int _id, string _name, EnumItemType _type)
    TYPE = _type;
    ID = _id;
    NAME = _name;

  public event PropertyChangedEventHandler PropertyChanged;
  public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

i have a list of items
List<Item> myItemsList = new List<Item>();
myItemsList.add(new Item(1, "1st", EnumItemType.NEW ));
myItemsList.add(new Item(1, "2nd", EnumItemType.OLD ));
myItemsList.add(new Item(1, "3rd", EnumItemType.NEW ));

I have a form that contains 2 combo boxes and 1 textbox

1st combo box (comboBoxId) shows the IDs of all the items in the list

The textbox (textBox1) shows the name of the item that is selected in the 1st combo box

The 2nd combo box (comboBoxType) shows the type of the item that is selected in the 1st combo box,
and allows the user to change its type - if the user clicks this combo box it should show all the available types of the item (that is all the EnumItemType values) and the user can change the type of the 1st combo box selected item.

To achieve this I tried using Binding

BindingList<Item> bindList = new BindingList<Item>();

for (i = 0 i < MyItemsList.Count; i++)

BindingSource bindSource = new BindingSource();
bindSource.DataSource = bindList;

comboBoxId.DataSource = bindSource;
comboBoxId.DisplayMember = "ID";

textBox1.DataBindings.Add("Text", bindSource, "NAME");

comboBoxType.DataSource = bindSource;
comboBoxType.DisplayMember = "TYPE";

Alas, -

I wanted that when the user chose the Item id from the comboBoxId
it will change the values of comboBoxType and textBox1 - and that is achieved by this code but if the user changes the comboBoxType it changes the comboBoxId and textbox1

And comboBoxType only shows the type of the 3 items that exist in the list
- it doesnt show RENEWED as a possible value.

Please Help me understand how to declare the binding of the comboBoxType so it will be connected to the selected item but wont change the selected item


What I have tried:

Declaring a seperate BindingSource to the comboBoxType - but i didnt know how to
connect it to the selected item in the list

Updated 26-Aug-21 4:15am
BillWoodruff 26-Aug-21 10:11am
Hi, seems obvious to me that you do not want to bind the second ComboBox ... binding it means it can only show the current list data, not all the values in the Enum.

1 solution

I found a solution:
The comboBoxType binding should be declared like that:

//Clicking the combo box now shows all the enum values 
comboBoxType.DataSource = Enum.GetValues(typeof(EnumItemType));

//the selected item of the combo box now bounded to the item type member of the
//item from the list
comboBoxType.DataBindings.Add("SelectedItem", bindSource, "ItemType");
BillWoodruff 26-Aug-21 15:22pm
Good technique ! +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900