How do i show enum values in a combo-box?

后端 未结 3 2019
说谎
说谎 2021-01-01 21:48

How do i show enum values in a combo-box? The code below result in the combobox having all displayed names being \"caseHandler.cState\". I wanted it to have the actual names

相关标签:
3条回答
  • 2021-01-01 22:13

    The Enum

    public enum Status { Active = 0, Canceled = 3 }; 
    

    Setting the drop down values from it

    cbStatus.DataSource = Enum.GetValues(typeof(Status));
    

    Getting the enum from the selected item

    Status status; 
    Enum.TryParse<Status>(cbStatus.SelectedValue.ToString(), out status); 
    

    http://amir-shenodua.blogspot.com/2012/03/net-using-enum-to-populate-combo-box.html

    0 讨论(0)
  • 2021-01-01 22:18

    Have you tried to use

    cbState.DataSource = Enum.GetNames(typeof(caseState));
    

    And when retrieving data just Parse it

    0 讨论(0)
  • 2021-01-01 22:18

    Here My Code , you can have text and value together and fill Combobox

     public enum LayerType : int
    {
    
        [Description("محوطه")]
        Area = 1,
    
    
        [Description("ساختمان")]
        Building = 2,
    
    
        [Description("بارانداز")]
        Wharf = 3,}
    
    drpLayer.DataSource = Enum.GetValues(typeof(LayerType))
                 .Cast<Enum>()
                 .Select(value => new
            {
            (Attribute.GetCustomAttribute(value.GetType().GetField(value.ToString()), typeof(DescriptionAttribute)) as DescriptionAttribute).Description,
            value
             })
            .OrderBy(item => item.value)
            .ToList();
                drpLayer.DisplayMember = "Description";
                drpLayer.ValueMember = "value";
    
    0 讨论(0)
提交回复
热议问题