具有共享数据源的独立下拉列表

时间:2011-07-13 11:20:15

标签: c# .net

我有绑定列表:

BindingList<string> sampleList = new BindingList<string>();
sampleList.Add("aaa");
sampleList.Add("bbb");

用作两个组合框的数据源:

comboBox1.DataSource = sampleList;
comboBox2.DataSource = sampleList;

当我在其中一个组合框中更改选择时,第二个也会受到影响。我怎样才能让他们独立?

编辑:

由于一些“热门”评论,我必须做一些澄清:

  • 是windows窗体代码
  • 可能
  • 背后没有其他逻辑/代码
  • 我正在使用.NET 2.0

完整代码来源:

public partial class Form1 : Form
{
    BindingList<string> sampleList;

    public Form1()
    {
        InitializeComponent();

        sampleList = new BindingList<string>();
        sampleList.Add("aaa");
        sampleList.Add("bbb");

        comboBox1.DataSource = sampleList;
        comboBox2.DataSource = sampleList;
    }
}

2 个答案:

答案 0 :(得分:1)

试试这个

comboBox1.DataSource = sampleList.ToList();
comboBox2.DataSource = sampleList.ToList();

答案 1 :(得分:0)

使用2个单独的数据源。在这种特殊情况下,每个组合有2个不同的BindingList个实例,可能是最好的解决方案,特别是如果我们讨论的是合理的小列表。

如果我没有错误,每个表单都有默认关联BindingSource,实际上,它管理指向同一集合的“链接”控件。但老实说,我不太确定。

问候。