Alex Rivera | Logout

Display enum in ComboBox with spaces

Asked 2009-07-09T05:45:30.550
15

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

c#
Edit
Report

1 Answer

4

Try this...

comboBox1.DataSource = Enum.GetValues(typeof(MyEnum))
                           .Cast<MyEnum>()
                           .Select(e => new { Value = e, Description = e.ToString().Replace("_"," ") })
                           .ToList();
comboBox1.DisplayMember = "Description";
comboBox1.ValueMember = "Value";

...although, I would be more inclined to use a "Description" attribute (as per Steve Crane's answer).

answered 2011-06-30T14:55:52.360

Your Answer