[英]when I change combobox1 it also changes the values in combobox2 (both use the same List)
I've 2 combobox on my winform. 我的Winform上有2个组合框。 Both comboboxes are loaded by the list below.
这两个组合框均由下面的列表加载。 Everything works fine.
一切正常。 Except, when I change a value in Combobox1 then it also changes the value in combobox2... and the same for other combobox.
例外,当我更改Combobox1中的值时,它也会更改combobox2中的值...其他组合框也是如此。 When I change a value in combobox 2 it changes in combobox1....
当我更改组合框2中的值时,它也会更改组合框1中的值。
Both have to use the same list of values. 两者都必须使用相同的值列表。 so that's the reason why I just bind to the same list (_item).
所以这就是为什么我只绑定到同一列表(_item)的原因。
so what do I need to do to decouple the 2 comboboxes from each other? 所以我需要怎么做才能使两个组合框彼此分离?
IList<CompteGeneral> _item = new List<CompteGeneral>(compt_repository.GetAll);
combobox1.DataSource = _item;
combobox1.DisplayMember = "AccountNumber";
combobox2.DataSource = _item;
combobox2.DisplayMember = "AccountNumber";
IList<CompteGeneral> _item = new List<CompteGeneral>(compt_repository.GetAll);
IList<CompteGeneral> _item1 = new List<CompteGeneral>(compt_repository.GetAll);
combobox1.DataSource = _item;
combobox1.DisplayMember = "AccountNumber";
combobox2.DataSource = _item1;
combobox2.DisplayMember = "AccountNumber";
or 要么
IList<CompteGeneral> _item = new List<CompteGeneral>(compt_repository.GetAll);
BindingSource source=new BindingSource();
source.DataSource=_item ;
BindingSource source1=new BindingSource();
source1.DataSource=_item ;
combobox1.DataSource = source;
combobox1.DisplayMember = "AccountNumber";
combobox2.DataSource = source1;
combobox2.DisplayMember = "AccountNumber";
Create a new List with the same item by passing the _item1
in the constructor. 通过在构造函数中传递
_item1
来创建具有相同项目的新List。
Assign the new list to the second Combobox. 将新列表分配给第二个组合框。
IList<CompteGeneral> _item1 = new List<CompteGeneral>(compt_repository.GetAll);
IList<CompteGeneral> _item2 = new List<CompteGeneral>(_item1);
combobox1.DataSource = _item1;
combobox1.DisplayMember = "AccountNumber";
combobox2.DataSource = _item2;
combobox2.DisplayMember = "AccountNumber";
implement the Clone method from ICloneable
interface on CompteGeneral
从
CompteGeneral
ICloneable
接口实现Clone方法
IList<CompteGeneral> _item = new List<CompteGeneral>(compt_repository.GetAll);
combobox1.DataSource = _item;
combobox1.DisplayMember = "AccountNumber";
combobox2.DataSource = _item.Select(p => p.Clone()).ToList();
combobox2.DisplayMember = "AccountNumber";
Also search for ShallowCopy and DeepCopy paradigms when cloning objects. 克隆对象时,还要搜索ShallowCopy和DeepCopy范例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.