【问题标题】:Binding two combo boxes to the same data source,that each combo will have individual behaviour将两个组合框绑定到同一个数据源,每个组合都有单独的行为
【发布时间】:2013-06-15 15:39:24
【问题描述】:

现在,当我在第一个组合框中选择项目时,第二个组合框模仿了选择。 我希望能够分别在每个组合框中进行选择。 谢谢。

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

【问题讨论】:

    标签: c# winforms binding combobox


    【解决方案1】:

    我不知道您使用的是哪种字典,但我使用的是普通字典,而且这段代码的行为不是这样:

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

    【讨论】:

    • 正是我所需要的
    猜你喜欢
    • 1970-01-01
    • 2016-08-23
    • 2019-08-15
    • 2012-09-01
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 2012-12-27
    相关资源
    最近更新 更多