Roby G.
Roby G.

Reputation: 105

add item combobox with datasource

I have a method (LoadCustomers()) that returns a dictionary of elements as follows:

Dictionary<int, string>()

I connected it to the datasource of a combobox like this:

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

Now I would like to put in front of the list of the combobox an item like:

             <select one customer>

How to do this in c# on winforms?

Tks a lot

Upvotes: 0

Views: 122

Answers (1)

Fabio
Fabio

Reputation: 32445

Add this option to the Dictionary of customers

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

Upvotes: 1

Related Questions