Click here to Skip to main content
13,736,491 members
Click here to Skip to main content
Add your own
alternative version

Stats

45.7K views
18 bookmarked
Posted 19 Aug 2009
Licenced CPOL

How to Fill a ListBox/DropDownList from an Enum

, 22 Aug 2009
Rate this:
Please Sign up or sign in to vote.
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

// ---- 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:

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.

License

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

Share

About the Author

Steve Wellens
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.

You may also be interested in...

Comments and Discussions

 
QuestionThis is very good article. Pin
Jayesh Sorathia11-Feb-13 3:15
memberJayesh Sorathia11-Feb-13 3:15 
SuggestionDemo with snap shot and attachment Pin
KrishanGahlot8-Oct-12 1:36
memberKrishanGahlot8-Oct-12 1:36 
GeneralEnumToListBox with one line of code. [modified] Pin
Paw Jershauge20-Aug-09 21:50
memberPaw Jershauge20-Aug-09 21:50 
GeneralRe: EnumToListBox with one line of code. Pin
Steve Wellens21-Aug-09 4:03
memberSteve Wellens21-Aug-09 4:03 
GeneralRe: EnumToListBox with one line of code. Pin
Paw Jershauge21-Aug-09 8:39
memberPaw Jershauge21-Aug-09 8:39 
GeneralRe: EnumToListBox with one line of code. Pin
Steve Wellens21-Aug-09 12:00
memberSteve Wellens21-Aug-09 12:00 
GeneralRe: EnumToListBox with one line of code. Pin
Paw Jershauge22-Aug-09 0:43
memberPaw 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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 22 Aug 2009
Article Copyright 2009 by Steve Wellens
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid