Click here to Skip to main content
12,952,443 members (38,670 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 25 Jul 2004

Enums as a DataSource

, 25 Jul 2004
Rate this:
Please Sign up or sign in to vote.
How to use an enum as a datasource for lists in .NET applications


This article presents a small (3) collection of classes that allow enums to be used as data sources to a combobox, listbox etc. The classes use reflection at runtime to generate a list of values that the combobox or listbox uses to full its items collection.


I was developing an application data model and started modelling object states as enums. When I came to developing the forms create or update these items I wanted those same enum values to be available for user selection. Not wanting to retype the the enum values I thought using reflection might be a cool way of solving the problem. Two options came to mind, derive from the control classes to allow enum types to be used as a DataSource or implement a collection class to supply the values. I chose the second because I thought a collection of enum name/value pairs would be more useful.

Using the code

Using the code is straightforward. After defining your enumeration type and control, instantiate a an instance of the EnumItemCollection class passing the type of your enum as parameter. The simply assign this object to the controls DataSouce property

// Extract from code sample
public class EnumDatasourceForm : System.Windows.Forms.Form
  private System.Windows.Forms.ComboBox colourCombo;
  private System.Windows.Forms.ListBox colourListBox;
  private System.Windows.Forms.CheckedListBox colourCheckedListBox;
  private System.Windows.Forms.Button button1;

  private AgileThought.Data.EnumItemCollection coloursCollection = 
    new AgileThought.Data.EnumItemCollection(typeof(Colours));
  private void EnumDatasourceForm_Load(object sender, System.EventArgs e)
    colourCombo.DataSource = coloursCollection;
    colourListBox.DataSource = coloursCollection;
    colourCheckedListBox.DataSource = coloursCollection;
  private void colourCombo_SelectedIndexChanged(object sender, System.EventArgs e)
    EnumPair selectedItem = (EnumPair)colourCombo.SelectedValue;
    MessageBox.Show(this, "You choose " + 
      selectedItem.Name + " " + selectedItem.Value);

The EnumPair class contains the enum name and value it can be retrieved from the SelectedValue property of the control.


  • 23/7/2004 First release


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Graham Brooks
Web Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralEnum class already does it Pin
Ulrich Lalk27-Jul-04 7:48
sussUlrich Lalk27-Jul-04 7:48 
GeneralRe: Enum class already does it Pin
Mr. Rogers28-Jul-04 11:23
memberMr. Rogers28-Jul-04 11:23 
QuestionRe: Enum class already does it [modified] Pin
Mauro Gagna4-Sep-06 5:51
memberMauro Gagna4-Sep-06 5:51 
AnswerRe: Enum class already does it Pin
Mauro Gagna4-Sep-06 6:29
memberMauro Gagna4-Sep-06 6:29 
GeneralModelSupport Pin
NoUserNameFound26-Jul-04 1:47
memberNoUserNameFound26-Jul-04 1:47 

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 | Terms of Use | Mobile
Web01 | 2.8.170525.1 | Last Updated 26 Jul 2004
Article Copyright 2004 by Graham Brooks
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid