Click here to Skip to main content
15,921,793 members
Articles / Web Development / ASP.NET

How to Fill a ListBox/DropDownList from an Enum

Rate me:
Please Sign up or sign in to vote.
4.13/5 (7 votes)
22 Aug 2009CPOL 49.8K   17   6
There was a question about this on the ASP.NET forums and after a quick search I didn't find a good generic function so I thought I'd supply one. Note: I wanted this to be as broad and useful as possible, so the second parameter is a ListControl which both the ListBox and DropDownList inherit from.

There was a question about this on the ASP.NET forums and after a quick search I didn't find a good generic function so I thought I'd supply one.

Note: I wanted this to be as broad and useful as possible, so the second parameter is a ListControl which both the ListBox and DropDownList inherit from.

I also made sure the function would handle enums that had non-contiguous values that didn't necessarily start at zero.

The Function

C#
// ---- EnumToListBox ------------------------------------
//
// Fills List controls (ListBox, DropDownList) with the text 
// and value of enums
//
// Usage:  EnumToListBox(typeof(MyEnum), ListBox1);

static public void EnumToListBox(Type EnumType, ListControl TheListBox)
{
    Array Values = System.Enum.GetValues(EnumType);

    foreach (int Value in Values)
    {
        string Display = Enum.GetName(EnumType, Value);
        ListItem Item = new ListItem(Display, Value.ToString());
        TheListBox.Items.Add(Item);
    }
}

Usage

I tested with an existing enum and a custom enum:

C#
enum CustomColors
{
    BLACK = -1,
    RED = 7,
    GREEN = 14,
    UNKNOWN = -13
}

protected void Button1_Click(object sender, EventArgs e)
{
    EnumToListBox(typeof(DayOfWeek), DropDownList1);

    EnumToListBox(typeof(CustomColors), ListBox1);
}

Note: I initially tried to get the order of the items in the ListBox to match the order of the items in the enum but both the Enum.GetValues and Enum.GetNames functions return the items sorted by the value of the enum. So if you want the enums sorted a certain way, the values of the enums must be sorted that way. I think this is reasonable; how the enums are physically sorted in the source code shouldn't necessarily have any meaning.

I hope someone finds this useful.

This article was originally posted at http://weblogs.asp.net/stevewellens/privaterss.aspx

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
EndWell Software, Inc.
United States United States
I am an independent contractor/consultant working in the Twin Cities area in Minnesota. I work in .Net, Asp.Net, C#, C++, XML, SQL, Windows Forms, HTML, CSS, etc., etc., etc.

Comments and Discussions

 
QuestionThis is very good article. Pin
Jayesh Sorathia11-Feb-13 3:15
Jayesh Sorathia11-Feb-13 3:15 
SuggestionMessage Removed Pin
8-Oct-12 1:36
KrishanGahlot8-Oct-12 1:36 
GeneralEnumToListBox with one line of code. [modified] Pin
Paw Jershauge20-Aug-09 21:50
Paw Jershauge20-Aug-09 21:50 
GeneralRe: EnumToListBox with one line of code. Pin
Steve Wellens21-Aug-09 4:03
Steve Wellens21-Aug-09 4:03 
The code I posted includes both the text and value of the enum in the html of the rendered ListBox/DropDownList.

The code you posted does not include the value so for specific scenarios it would not work (for example: inserting the values to a database, Javascript manipulation of the values, etc.).

In addition, the code I posted allows control over how the enums are sorted in the listcontrols. Your code sorts alphabetically so for instance, doing the DayOfWeek enum results in a list sorted like this:
Friday
Saturday
Sunday
Monday
Thursday
Tuesday
Wednesday

No user would accept that.

Steve Wellens

GeneralRe: EnumToListBox with one line of code. Pin
Paw Jershauge21-Aug-09 8:39
Paw Jershauge21-Aug-09 8:39 
GeneralRe: EnumToListBox with one line of code. Pin
Steve Wellens21-Aug-09 12:00
Steve Wellens21-Aug-09 12:00 
GeneralRe: EnumToListBox with one line of code. Pin
Paw Jershauge22-Aug-09 0:43
Paw Jershauge22-Aug-09 0:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.