Display enum in ComboBox with spaces
c#
Solution
If you have access to the Framework 3.5, you could do something like this:
Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>()
.Select(e=> new
{
Value = e,
Text = e.ToString().Replace("_", " ")
});
This will return you an IEnumerable of an anonymous type, that contains a Value property, that is the enumeration type itself, and a Text property, that will contain the string representation of the enumerator with the underscores replaced with space.
The purpose of the Value property is that you can know exactly which enumerator was chosen in the combo, without having to get the underscores back and parse the string.
Problem
I have an enum, example: ``` enum MyEnum { My_Value_1, My_Value_2 } ``` With : ``` comboBox1.DataSource = Enum.GetValues(typeof(MyEnum)); ``` But now my question: How can I replace the "_" with " " so that it becomes items with spaces instead of underscores? And that a databound object still works