Binding enum to a ComboboxBinding enum to a Combobox
Old forum URL: forums.lhotka.net/forums/t/1203.aspx
sune42 posted on Wednesday, September 13, 2006
Hi
I have a CSLA object that has a Enum as one of its properties.
I then try to make a "Master-details" editor for this object but I have touble to bind the Enum property properly to a Drop-Down box.
Is it possible?
The first question is how the ComboBox is populated with all the possible Enum types??
Or should I instead create a separate Integer representation property of the enum and then manually map it to a not data-bound ComboBox?
RRorije replied on Wednesday, September 13, 2006
Yes, this is possible
This function creates an arraylist from an arbitrary enumaration, which can be bound to a combobox:
Friend Shared Function EnumToArray(ByVal type As Type) As ArrayList
Dim _EnumList As New ArrayList
For Each _item As Integer In [Enum].GetValues(type)
_EnumList.Add(New DictionaryEntry(_item, [Enum].GetName(type, _item)))
Next
Return _EnumList
End Function
Hope this helps
sune42 replied on Wednesday, September 13, 2006
Thousand thanks!!!!
//Andy
skagen00 replied on Wednesday, September 13, 2006
I'm actually moving away from Enums myself as I found myself exposing them as properties to business objects, binding them to comboboxes (and hence without spaces in the value descriptions)...
I've come to appreciate that perhaps that I may start shying away from Enums myself and moving more towards NameValueLists (well, my equivalent) with zero to many values represented as constants within the "NVL" class to function like an enum w/ intellisense, etc. I get a more consistent approach - binding "code option trees" (my NVL) to comboboxes/dropdowns throughout - without some of the "no/nos" of using enums as properties of BOs and the quirkiness with binding them to comboboxes and dropdownlists.
My two cents...
(There are a lot of things enums make a lot of sense for, don't get me wrong!)
Brian Criswell replied on Wednesday, September 13, 2006
You can do this:
comboBox1.DataSource = Enum.GetValues(typeof(MyEnum));
Brad replied on Wednesday, September 13, 2006
sune42 wrote:
"The first question is how the ComboBox is populated with all the possible Enum types??"
There have been some excellent suggestions posted in this thread if you require to bind the actual enumeration values to the combo box. On some occasions, I have needed some extra UI customisation of those bound enum values (i.e. I may require to bind the enum value "TurnLeft" - but actually want "Turn Left" displayed to the user). The following blog post I wrote gives some detail about how I went about this:
http://www.dotnetgeeks.com/blogs/bleach/archive/2006/07/22/Enumeration-Binding.aspxHope this helps.
-Brad
Copyright (c) Marimer LLC