Combobox filled with Enum

How can I bind specific Enumto Combobox?

public enum EduTypePublicEnum
  {
    [RMSEnumItem("1", "Properties.Resources.SEduAlumn")]
    Alumn,
    [RMSEnumItem("2", "Properties.Resources.SEduProfesor")]
    Profesor,
    [RMSEnumItem("3", "Properties.Resources.SEduAll")]
    All
  }

  public class EduTypePublic : RMSEnum<EduTypePublicEnum> { };

In my form

public EduAvisosForm()
{
     InitializeComponent();

     this.myComboBox.DataSource = Edu.Consts.EduTypePublic.Enums;
     this.myComboBox.DisplayMember = "Alumn";
     this.myComboBox.ValueMember = "Alumn";
}

But with ValueMemberor without an error, an error occurs. When I put this code without ValueMember, the error requesting ValueMemberwhen I put it does not work.

"Is not possible define SelectedValue in a ListControl with empty ValueMember"

public abstract class RMSEnum<TEnumType>
    {
        protected RMSEnum();

        public static string CodeList { get; }
        public static string[] Codes { get; }
        public static string DescriptionList { get; }
        public static string[] Descriptions { get; }
        public static object[] Enums { get; }

        public static string Code(TEnumType value);
        public static string Description(string code);
        public static string Description(TEnumType value);
        public static TEnumType Enum(string code);
    }
+3
source share
1 answer

Data source elements must have properties that you specify as display elements and values. For instance. The KeyValuePairs dictionary has properties named Key and Value:

 this.myComboBox.DisplayMember = "Value";
 this.myComboBox.ValueMember = "Key";
 this.myComboBox.DataSource = 
      Enum.GetValues(typeof(EduTypePublicEnum))
          .Cast<EduTypePublicEnum>()
          .Select(e => new { 
               Key = e.ToString(), // possibly read localized string
               Value = e
           }).ToList();
0
source

All Articles