Reputation: 513
I have a windows form with many comboboxes. They all have to display the same items and I want to be able to remove items from their list of values. So I decided to try and make a List variable so I could easily remove and insert values into it. So what I did was
List<string> Ranks = new List<string>(new string[] { "values here" });
Then in my Form1Designer.cs
this.ComboBox_Rank_0.DataSource = Ranks;
I heart ValueMember and DisplayMember were good things to have, but so far it works without them. When compiled the form loads and the comboboxes have the correct values.
The problem is that when I choose a value in one combobox the others get the same value selected, as well. Any ideas?
Upvotes: 1
Views: 683
Reputation: 216263
Use a BindingList<T>
This class allows you to handle the interaction with your list separately for each of the combos
For example
List<string> names = new List<string>()
{"Steve", "Mark", "Luke", "John", "Robert"};
BindingList<string> bl1 = new BindingList<string>(names);
ComboBox_Rank_0.DataSource = bl1;
BindingList<string> bl2 = new BindingList<string>(names);
ComboBox_Rank_1.DataSource = bl2;
The BindingList<T>
requires using System.ComponentModel;
and notice that you don't require the new string[] syntax in the constructor of your list
Upvotes: 2