exceptionz

Thoughts on Technology, Methodology and Programming.

How do you bind a Combobox to an Enum Property?

Posted by Marcus Wyatt on 14 May 2005

Edit: I’ve updated the code and post here.

Probably many developers have worked with win forms data binding and wanted to do the following:

Data bind a Combobox to an enum in your domain.

Here is my solution to the problem:

We create a ComboBoxEnumItem Class. This class is a pure fabrication class that has two properties we’ll use when databinding to the combo. We have Value which is used by the ValueMember and DisplayItem that is used by the DisplayMember of the combo. Note that when we construct a new ComboBoxEnumItem instance that we call the AsciiEscape method when populating the displayItem field.

using System.Text.RegularExpressions;

public class ComboBoxEnumItem
{
   private object value;
   private string toString;
   private string displayItem;

   public object Value
   {
      get
      {
         return value;
      }
   }

   public override string ToString()
   {
      return toString;
   }

   public string DisplayItem
   {
      get
      {
         return displayItem;
      }
   }

   public ComboBoxEnumItem( object value )
   {
      this.value = value;
      displayItem = AsciiEscape( value.ToString() );
      toString = displayItem;
   }

   private string AsciiEscape( string value )
   {
      if( value == null )
         return null;

      string returnValue = "";
      Regex r = new Regex("0x[0-9A-Z]{4}");
      MatchCollection m = r.Matches( value );
      Match mtchPrevious = null;

      for( int i = 0; i < m.count; i++ )
      {
         int istartindex = (mtchprevious == null ? 0: mtchprevious.index + mtchprevious.length );
         returnvalue += value.substring( istartindex, m[i].index - istartindex );
         string strgroup = m[ i ].value;

         if( strgroup.startswith("0x") )
         {
            string strcode = strgroup.substring(2);
            byte bval = system.convert.tobyte( strcode, 16 );
            string s = new string((char)bval,1);
            returnvalue += s;
         }

         mtchprevious = m[i];
      }

      if ( mtchprevious == null )
      {
         returnvalue += value;
      }
      else
      {
         returnvalue += value.substring( mtchprevious.index + mtchprevious.length );
      }

      return returnvalue;
   }
}

The AsciiEscape method is used to Replace the Ascii characters in our Enum with the character representation. We do the Ascii characters because we want our combo to display “Bi- Annual” instead of “BiAnnual”. Look at the following enum as an example:

public enum PaymentFrequencies
{
   Annual = 1,
   Bi0x00A00x002D0x00A0Annual = 2,
   Quarterly = 4, Monthly = 12,
   Fortnightly = 26,
   Weekly = 52
}

Now, all that we need is the following code behind the form to bind our Combobox and we are away:

private void InitDataBindings()
{
   cboEntity.DataSource = GetEnumComboDataSource( PaymentFrequencies );
   cboEntity.DisplayMember = "DisplayItem";
   cboEntity.ValueMember = "Value";
   cboEntity.DataBindings.Add( "SelectedValue", LoanLogic, "PaymentFrequency" );
}

private ArrayList GetEnumComboDataSource( Type type )
{
   ArrayList comboData = new ArrayList();
   Array ary = Enum.GetValues( type );

   for( int j = 0; j < ary.length; j++ )
   {
      comboData.Add( new ComboBoxEnumItem( ary.GetValue( j ) ) );
   }

   return comboData;
}

The GetEnumComboDataSource method creates an ArrayList for us to use as a data source for the combobox.

And there you have it, a Combobox bound to an Enum!

tags: , ,

Currently listening to: 432. delerium – underwater – Winamp ***

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>