Assigning key value pairs to a combo box Recently someone asked me how to assign values to a combo box in .NET. The request seemed simple enough, but it can often confuse someone new to the .NET framework. To help them, and others following is a quick rundown on some of the options available.
Loop values
You can for loop through a list of values and add each item individually.string[] colours = new string[]
This will assign colours "Red", "Green", "Yellow" to a combo box named combobox1.
{
"Red",
"Green",
"Yellow"
};
foreach(string colour in colours)
{
combobox1.Items.Add(colour);
}Object Properties
You can utilise the combobox's datasource property to load an array of objects.string[] colours = new string[]
Again, this will assign colours "Red", "Green", "Yellow" to a combo box named combobox1, but its much simpler. This method uses the object's ToString() function to determine the display text for each item.
{
"Red",
"Green",
"Yellow"
};
combobox1.DataSource = colours;Object without ToString()
Method two above will work in situations where the object's value returns a human readable ToString() value. However, this is not always the case, to work around this, we can utilise the combobox's DisplayMember property. Take a look at the below example, I am creating a new class 'Employee' which has two properties - ID and Name.class Employee
When an array of employees is assigned to a combo box object, it will only display the object type not Employee Name.
{
private int _id;
private string _name;
public Employee(int id, string name)
{
_id = id;
_name = name;
}
public int ID
{
get
{
return _id;
}
}
public string Name
{
get
{
return _name;
}
}
}Employee[] employees = new Employee[2];
To overcome this we can set the DisplayMember property of the combobox to the property of the object which I want to show in the combobox, in this case 'Name'.
employees[0] = new Employee(1, "Jane Smith");
employees[1] = new Employee(2, "John Smith");
combobox1.DataSource = employees;combobox1.DisplayMember = "Name";
This will show 'Jane Smith' and 'John Smith' in the combobox drop down list.Different value and display items
In example 3, I was able to display 'Jane Smith' and 'John Smith' in the combo box. However, the ID value is the primary key for the Employee data and as such we need to utilise it in other code, and need easy access to it. In this case we can use the ValueMember property load the value for each entry. Taking the Employee class from example 3 we can createEmployee[] employees = new Employee[2];
This will result in the property "ID" being paired with each combo box item, and available as 'combobox1.SelectedValue' property.
employees[0] = new Employee(1, "Jane Smith");
employees[1] = new Employee(2, "John Smith");
combobox1.DataSource = employees;
combobox1.DisplayMember = "Name";
combobox1.ValueMember = "ID";Linking directly to tables
At times it is necessary to load data to a combobox from a database. We can either convert the returned data into an array or object and assign it to the combobox via the datasource property. However, you can use the same principal to map a data table to a combobox object. If we have a data table 'dt' that is filled from the database with the employee values.
We can assign the table directly to the combobox and use the principals from example 4 to assign values.ID Name 1 Jane Smith 2 John Smith SqlDataAdapter da = new SqlDataAdapter(
This will produce the same results as example 4, but it will be loaded dynamically from the database.
new SqlCommand("GetEmployees"),
"datbaseconnection");
DataTable dt = new DataTable();
da.Fill(dt);
combobox1.DataSource = dt;
combobox1.DisplayMember = "Name";
combobox1.ValueMember = "ID";
Disclaimer: The content provided in this article is not warranted or guaranteed by rnddev. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts. As such it is inferred on to the reader to employ real-world tactics for security and implementation of best practices. I am not liable for any negative consequences that may result from implementing any information covered in this or other articles or blog posts.