使用数据源添加项组合框

本文关键字:组合 添加 数据源 | 更新日期: 2023-09-27 18:36:12

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

Dictionary<int, string>()

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

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

现在我想在组合框列表的前面放一个项目,例如:

             <select one customer>

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

很多

使用数据源添加项组合框

将此选项添加到客户字典

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