Binding an enum to a WinForms combo box, and then setting it

前端 未结 28 2158
心在旅途
心在旅途 2020-11-28 04:33

a lot of people have answered the question of how to bind an enum to a combo box in WinForms. Its like this:

comboBox1.DataSource = Enum.GetValues(typeof(MyE         


        
28条回答
  •  悲&欢浪女
    2020-11-28 04:45

    A little late to this party ,

    The SelectedValue.ToString() method should pull in the DisplayedName . However this article DataBinding Enum and also With Descriptions shows a handy way to not only have that but instead you can add a custom description attribute to the enum and use it for your displayed value if you preferred. Very simple and easy and about 15 lines or so of code (unless you count the curly braces) for everything.

    It is pretty nifty code and you can make it an extension method to boot ...

提交回复
热议问题