Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 of the enum values.

My enum is defined as follows:

public enum caseState
{
    Active = 1,
    Finished,
    Problem
}

I have a class that is defined as this:

public class cState
{    
    public string _name;
    public int _id;

    public cState(int id,string name)
    {
        _name = name;
        _id = id;
    }
}

And the code for populating my combobox:

ArrayList AL = new ArrayList();

foreach (string cs in Enum.GetNames(typeof(caseState)))
{
    cState aEnum = new cState((int)Enum.Parse(typeof(caseState),cs),cs);
    AL.Add(aEnum);
}


cbState.DisplayMember = "_name";
cbState.ValueMember = "_id";

cbState.DataSource = AL;
like image 551
Bildsoe Avatar asked Feb 21 '11 11:02

Bildsoe


People also ask

How do I display the value of an enum?

An enumeration is a great way to define a set of constant values in a single data type. If you want to display an enum's element name on your UI directly by calling its ToString() method, it will be displayed as it has been defined.

Can you give enums values?

By default enums have their own string values, we can also assign some custom values to enums.


1 Answers

Have you tried to use

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

And when retrieving data just Parse it

like image 110
Stecya Avatar answered Sep 21 '22 15:09

Stecya