Click here to Skip to main content
Click here to Skip to main content

Enums as a DataSource

By , 25 Jul 2004
Rate this:
Please Sign up or sign in to vote.

Introduction

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.

Background

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.

History

  • 23/7/2004 First release

License

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

Comments and Discussions

 
GeneralEnum class already does it PinsussUlrich Lalk27-Jul-04 7:48 
GeneralRe: Enum class already does it PinmemberMr. Rogers28-Jul-04 11:23 
QuestionRe: Enum class already does it [modified] PinmemberMauro Gagna4-Sep-06 5:51 
AnswerRe: Enum class already does it PinmemberMauro Gagna4-Sep-06 6:29 
GeneralModelSupport PinmemberNoUserNameFound26-Jul-04 1:47 

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.

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 26 Jul 2004
Article Copyright 2004 by Graham Brooks
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid