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

夙愿已清 提交于 2019-12-21 05:08:10

问题


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?


回答1:


Or you could use...

var dataSource = new[] { "item1", "item2", "item3" };
comboBox1.DataSource = dataSource;
comboBox2.BindingContext = new BindingContext();
comboBox2.DataSource = dataSource;



回答2:


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();


来源:https://stackoverflow.com/questions/1729873/how-to-separate-the-selected-item-of-two-combobox-with-a-single-datasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!