Hi
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
{
NEW,RENEWED,OLD
}
class Item : INotifyPropertyChanged
{
private EnumItemType m_eType;
public EnumItemType TYPE
{
get => m_eType;
set
{
m_eType= value;
NotifyPropertyChanged();
}
}
private int m_iId;
public int ID
{
get => m_iId;
set
{
m_iId = value;
NotifyPropertyChanged();
}
}
private string m_sName;
public string NAME
{
get => m_sName;
set
{
m_sName= value;
NotifyPropertyChanged();
}
}
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++)
{
bindList.add(myItemsList[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
also!
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
Thanks!
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
googling...