Click here to Skip to main content
Licence CPOL
First Posted 19 Aug 2009
Views 17,418
Bookmarked 17 times

How to Fill a ListBox/DropDownList from an Enum

By | 22 Aug 2009 | Technical Blog
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.
A Technical Blog article. View original blog here.[^]

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)

About the Author

Steve Wellens


EndWell Software, Inc.
United States United States

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralEnumToListBox with one line of code. [modified] PinmemberPaw Jershauge21:50 20 Aug '09  
GeneralRe: EnumToListBox with one line of code. PinassociateSteve Wellens4:03 21 Aug '09  
GeneralRe: EnumToListBox with one line of code. PinmemberPaw Jershauge8:39 21 Aug '09  
GeneralRe: EnumToListBox with one line of code. PinassociateSteve Wellens12:00 21 Aug '09  
GeneralRe: EnumToListBox with one line of code. PinmemberPaw Jershauge0:43 22 Aug '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.5.120517.1 | Last Updated 22 Aug 2009
Article Copyright 2009 by Steve Wellens
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid