For the Access developer trying to make a transition to the .NET world, binding in general can be a difficult thing to wrap the head around. So much of this was handled behind the scenes in Access, when we start playing in .NET, we’re really up against a bit of a learning curve on binding. Not that it’s a bad thing per se – .NET binding offers a lot more options than we have in Access.

One thing in particular that takes a bit more of a walk off our beaten path is using a combobox in conjunction with some Enum values. In Access, we’d simply set our rowsource to whatever list we wanted, set a bound column (usually hiding it), then bind it right to the underlying recordset, or in VBA we’d have a direct translation from an Enum value to the bound column of the combo. In .NET, the process takes considerably more code when using simple binding, and we’ve certainly got better things to be doing with our time than plumbing comboboxes to match up to an enum every time we have the need.

The following provides a means to turn an enum into something a combobox can understand. Then, with the DisplayMember and ValueMember of the combobox set properly, we can directly bind to an object whose property is an enum. Optionally, there’s a FormatString method that will take the enum literal and turn it from a CamelCaseString to a “normal” spaced string (Camel Case String). This is much more user friendly for showing up as list items in the combobox, and in many cases we’ll be able to get away with using the CamelCaseEnumValue to display Camel Case Enum Value without having to build yet another list for displaying.

Here’s the method that does the magic on the enum itself (adopted from this SO thread). You’ll note that this also includes a method that will parse the string representation of the enum values into the Non Camel Case format.

Let’s pretend we have an enum that provides a FileSyncType:

To use this, we’ll simply call the EnumUtil.ListOf<> method, passing the FileSyncType enum itself through the call. Apply the return directly to the DataSource property of the combobox:

You’ll notice that the ListOf<> method returns a KeyValuePair object. We’ll apply the Value (which is the string representation of the enum) to the DisplayMember of the combobox, and the Key (the actual value of the enum: think backwards!) to the ValueMember of the combobox. Then all that’s left if to apply a binding to the source object which contains the property for the FileSyncType (binding to the SelectedValue property of the combobox):

In other binding scenarios this implementation may not be needed (binding to a database, for example), but for custom object binding to an enum’d property, this certainly beats the manual plumbing that would otherwise be required for every combobox we wanted to set up.