我有两个都使用相同数据源的组合框。每当我更改组合框之一时,其他组合框就会更改为完全相同的值。
看来要解决此问题,我需要使用数据绑定。
我尝试了很多事情,但似乎没有任何效果。我的组合框分别命名为comboBox1和comboBox2。以下是我试图用来使combobox2独立于comboBox1起作用的源代码。任何帮助将不胜感激。
BindingSource bs = new BindingSource(this.claimTypeBindingSource, null);
BindingContext bc = new BindingContext();
comboBox2.BindingContext = bc;
comboBox2.DataSource = bs.DataSource;
comboBox2.DisplayMember = "ClaimType";
编辑我现在刚刚从comboBox1中取出了dataSource分配,并使用代码进行了分配-代码现在看起来像这样。
private void Form1_Activated(object sender, EventArgs e)
{
BindingSource bs1 = new BindingSource(this.claimTypeBindingSource, null);
BindingContext bc1 = new BindingContext();
comboBox1.BindingContext = bc1;
comboBox1.DataSource = bs1.DataSource;
comboBox1.DisplayMember = "ClaimType";
BindingSource bs2 = new BindingSource(this.claimTypeBindingSource, null);
BindingContext bc2 = new BindingContext();
comboBox2.BindingContext = bc2;
comboBox2.DataSource = bs2.DataSource;
comboBox2.DisplayMember = "ClaimType";
}
这并没有解决问题。如果我更改1 comboBox,其他的也将更改。
我刚刚解决了它:-)这有效-我已经将其放入我的真实项目中,并且效果很好。
private void Form1_Activated(object sender, EventArgs e)
{
comboBox1.DataSource = new BindingSource(this.claimTypeBindingSource, null);
comboBox1.DisplayMember = "ClaimType";
comboBox2.DataSource = new BindingSource(this.claimTypeBindingSource, null);
comboBox2.DisplayMember = "ClaimType";
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句