1

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?

mathgenius
  • 503
  • 1
  • 6
  • 21
  • The issue is not because of using `List` and using `BindingList` doesn't make any difference. The problem is using a single list as data source which means the controls are sharing a single `BindingManagerBase`. As solution you can use multiple lists (including binding list or yourList.ToList) as data source. Or youcan use multuple `BindingSource` as data source. Or you can use new `BindingContext` for combo boxes. – Reza Aghaei Apr 07 '16 at 22:07
  • @RezaAghaei, well, uh... Steve's solution of using BindingLists worked just fine. – mathgenius Apr 10 '16 at 13:59
  • Surely it works properly and it's because of multiple `BindingList`. As I said the important thing here is: in this case there is no difference between `List` and `BindingList`. And what I said is not against the Steve's answer. But I described what is the actual reason of the problem and I shared 3 options to solve the problem. Take a look at this post, and read the post and test the solutions: [Multiple ComboBox bind to a Single List - Issue: When I choose an item, all combo boxes changes](http://stackoverflow.com/a/35865838/3110834). Surely you will find the post helpful:) – Reza Aghaei Apr 10 '16 at 16:05
  • By the way, I removed my answer and used the content to enhance the other answer because I think we need such answer to a question which I have seen 2-3 times recently, An answer which describes the reason and more solutions. Also I posted the comment under your question to share some useful information with future readers :) – Reza Aghaei Apr 10 '16 at 16:05
  • @RezaAghaei, thank you for your effort! I am looking into the other question/answer, now. :) – mathgenius Apr 16 '16 at 12:39
  • You're welcome, hope you find it helpful :) – Reza Aghaei Apr 16 '16 at 13:16

1 Answers1

2

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

Steve
  • 213,761
  • 22
  • 232
  • 286
  • What is the diff between using new BindingList(names) and new List(names)? – Bassem Apr 02 '16 at 23:16
  • Oh, I see, this is indeed good information. But I am also curious why it dies not need a "new string[]" this way. I will flag your answer correct as soon as I get to compile it! – mathgenius Apr 03 '16 at 00:25
  • About the _new string[]_ [Object and Collection Initializers](https://msdn.microsoft.com/en-us/library/bb384062.aspx), about `List` versus `BindingList` see: http://stackoverflow.com/questions/2243950/listt-vs-bindinglistt-advantages-disadvantages – Steve Apr 03 '16 at 08:43