开发者

Independent drop down lists with shared data source

I have binding list:

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

used as data source for two combo boxes:

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

When I change selection in one of these combo boxes, 开发者_开发技巧second is affected as well. How can I keep them independent?

EDIT:

Due to some 'hot' comments I have to do some clarifications:

  • it is windows forms code
  • it is possible
  • there is no another logic / code behind it
  • I'm using .NET 2.0

full code source:

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;
    }
}


try this

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


Use 2 separate Data sources. In this particular case 2 different BindingList instances per combo, may be the best solution, especially if we are talking about reasonably small lists.

If I'm not mistake every Form has it's default associated BindingSource and it, actually, manages the "linking" controls refered to the same collection. But I'm not very sure in this, honestly.

Regards.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜