C# – Auto-populate a dropdown with enum values

When you need to show enum values in a dropdown, it’s a good idea to automatically populate the list, instead of manually setting all of the values.

To autopopulate the dropdown, set the DataSource to Enum.Values(), like this:

dropDownListPets.DataSource = Enum.GetValues(typeof(Pets));
Code language: C# (cs)

Then to get the option the user picked, do the following:

var choice = (Pets)dropDownListPets.SelectedItem;
Code language: C# (cs)

When I launch my form, I can see it correctly populated the dropdown from my Pets enum:

Pets dropdown showing values from Pets enum

Leave a Comment