If I bind a WinForms ComboBox to an enum type’s values, i.e.
combo1.DropDownStyle = ComboBoxStyle.DropDownList;
combo1.DataSource = Enum.GetValues(typeof(myEnumType));
Who knows how I could achieve the same result, while, in addition to entries matching each enum value, I can also have a blank entry representing no selection?
I cannot simply add a special value to the enum type because this must be flexible to deal with any enum type.
I’d appreciate your help.
Edit: I should make clear that I want to bind the actual enum values and not their names. If the actual enum values are bound, the ComboBox takes care of calling their ToString() to get the text to display.
(Please see my edit to the question where I clarified that I don’t want to bind to a collection of strings).
After more fiddling, the following monstrosity seems to work. combo1.SelectedItem is of type object and will either be a DBNull or a (boxed?) enum value. Is this code advisable?
Edit: I see Adam and Andrew’s methods could easily be adapted to do the same thing. Thanks guys!