C# - Binding ComboBox with Enum
Bind the values of an enum to a WinForms ComboBox with a display name and a numeric key.
This snippet binds enum values to a ComboBox. Each item shows the enum name and carries the numeric value as its key.
csharp
public enum RequestDept
{
CallCenter = 0,
Development = 1,
Enrollment = 2,
Implementation = 3,
Sales = 4,
Support = 5
}
// Binding
comboBox1.DataSource = Enum.GetValues(typeof(RequestDept))
.Cast<RequestDept>()
.Select(p => new { Key = (int)p, Value = p.ToString() })
.ToList();
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
// Selecting an item
comboBox1.SelectedValue = 3;
Example
After binding, the list shows CallCenter, Development, Enrollment, Implementation, Sales and Support. Setting SelectedValue = 3 selects Implementation.