【问题标题】:add item combobox with datasource使用数据源添加项目组合框
【发布时间】:2015-03-03 06:34:25
【问题描述】:

我有一个方法 (LoadCustomers()),它返回一个元素字典,如下所示:

Dictionary<int, string>()

我将它连接到组合框的数据源,如下所示:

             Myclass m = new Myclass();
             combo1.DataSource = new BindingSource(m.LoadCustomers(), null);
             combo1.DisplayMember = "Value";
             combo1.ValueMember = "Key";

现在我想在组合框列表的前面放一个类似的项目:

             <select one customer>

如何在 c# 中在 winforms 上执行此操作?

非常感谢

【问题讨论】:

    标签: c# winforms binding combobox


    【解决方案1】:

    将此选项添加到客户词典

    const int EMPTYCUSTOMERKEY = -1;  //be sure Customers will not contain this value
    const string EMPTYCUSTOMERVALUE = "<select one customer>";
    
    Myclass m = new Myclass();
    Dictionary<int, string> customerSource = m.LoadCustomers();
    
    customerSource.Add(EMPTYCUSTOMERKEY, EMPTYCUSTOMERVALUE);
    
    combo1.DataSource = new BindingSource(customerSource, null);
    combo1.DisplayMember = "Value";
    combo1.ValueMember = "Key";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-15
      • 2013-12-17
      相关资源
      最近更新 更多