Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate the selected item of two combobox with a single DataSource?

On a form, I have two combobox wich have the same DataSource (their elements list are the same). When the user select an item in one of the control, the other control's selected item is also modified. That's not what I want.

I'd like to have both list populated with the same DataSource (as I currently do), but I'd like their selected items to be independent from each other.

How can I do that?

like image 980
Mathieu Pagé Avatar asked Nov 13 '09 15:11

Mathieu Pagé


2 Answers

Or you could use...

var dataSource = new[] { "item1", "item2", "item3" };
comboBox1.DataSource = dataSource;
comboBox2.BindingContext = new BindingContext();
comboBox2.DataSource = dataSource;
like image 197
Peter Rennie Avatar answered Oct 10 '22 15:10

Peter Rennie


You need to create two different instances of the data source. For this you may use the ToArray extension method:

var dataSource = new string[] { "item1", "item2", "item3" };
comboBox1.DataSource = dataSource.ToArray();
comboBox2.DataSource = dataSource.ToArray();
like image 36
Darin Dimitrov Avatar answered Oct 10 '22 15:10

Darin Dimitrov