Home > C# > C# – Binding ComboBox with Enum

C# – Binding ComboBox with Enum

A simple snippet that can be used to bind enum values to a combobox control

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;

Advertisements
Categories: C# Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: