I’m currently working on a Windows Forms GUI and I have a Combo that I need to display a list of string values as the DisplayMembers and use a list of user defined enum values as the ValueMember. I’m currently returning a List> from my database access function and I would like to bind this to my Combo box. I’ve tried assigning the list to the .DataSource property, assigning “Key” to .DataMember and “Value” to .DisplayMember. This is clearly not a valid approach as it is not working.
Can someone please give me another approach that is in good form and actually works?
Thanks
I do use my own class
EnumPair<>in combination with two extension methods to bind comboboxes to Properties with enum types.See if this can help you, that you can work directly with the enums.
Use it like this after implementation:
That assumes you have a ComboBox named “comboBox” on your form, an Enum called “MyEnumType” and an instance of a BindingSource. The PropertyNameOfBindingSource should be the name of the Property of the type that your BindingSource has a list of, that has the PropertyType of MyEnumType.
Implementation for the background work is found below, the extension methods are not needed, i just do not like write nearly identical lines of code 😉