Binding two combo boxes to the same data source,that each combo will have individual behaviour

淺唱寂寞╮ 提交于 2019-12-12 02:14:20

问题


Now when i select item in first combo box the second mimic the selection. I want to be able to select in each of the combo boxes individually. Thanks.

 List<W6AdminUIs2.DictionaryObject> taskStatuses = W6AdminUIs2.GlobalFunctions.GetDictionaryItems("TaskStatus");

    // Init the binding source of the statuses.
    BindingSource bsFromStatuses = new BindingSource();
    bsFromStatuses.DataSource = taskStatuses;

    //Bind the "From" combo box to binding source.
    cBoxFrom.DataSource = bsFromStatuses.DataSource;
    cBoxFrom.DisplayMember = "Name";
    cBoxFrom.ValueMember = "Key";

    // Init the binding source of the statuses.
    BindingSource bsToStatuses = new BindingSource();
    bsToStatuses.DataSource = taskStatuses;

    //Bind the "From" combo box to binding source.
    cBoxTo.DataSource = bsToStatuses.DataSource;
    cBoxTo.DisplayMember = "Name";
    cBoxTo.ValueMember = "Key";

回答1:


I don't know what kind of dictionary you are using but i use normal dictionary and this code dont behave like that :

Dictionary<string,string> dict = new Dictionary<string, string>();
dict.Add("S1", "Sample1");
dict.Add("S2", "Sample2");
dict.Add("S3", "Sample3");
dict.Add("S4", "Sample4");

comboBox1.DataSource = new BindingSource(dict, null);
comboBox1.DisplayMember = "value";
comboBox1.ValueMember = "key";

comboBox2.DataSource = new BindingSource(dict, null);
comboBox2.DisplayMember = "value";
comboBox2.ValueMember = "key";


来源:https://stackoverflow.com/questions/17185560/binding-two-combo-boxes-to-the-same-data-source-that-each-combo-will-have-indivi

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