Click here to Skip to main content
15,880,796 members
Articles / Programming Languages / C#
Article

Enums as a DataSource

Rate me:
Please Sign up or sign in to vote.
2.17/5 (8 votes)
25 Jul 20041 min read 64.5K   1.3K   18   5
How to use an enum as a datasource for lists in .NET applications

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

C#
//
// 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


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralEnum class already does it Pin
Ulrich Lalk27-Jul-04 7:48
Ulrich Lalk27-Jul-04 7:48 
GeneralRe: Enum class already does it Pin
Mr. Rogers28-Jul-04 11:23
Mr. Rogers28-Jul-04 11:23 
QuestionRe: Enum class already does it [modified] Pin
Mauro Gagna4-Sep-06 5:51
Mauro Gagna4-Sep-06 5:51 
AnswerRe: Enum class already does it Pin
Mauro Gagna4-Sep-06 6:29
Mauro Gagna4-Sep-06 6:29 
GeneralModelSupport Pin
NoUserNameFound26-Jul-04 1:47
NoUserNameFound26-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.