如何在组合框中显示枚举值? [英] How do i show enum values in a combo-box?

查看:148
本文介绍了如何在组合框中显示枚举值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在组合框中显示枚举值?下面的代码导致组合框中所有显示的名称为caseHandler.cState。我想要它有枚举值的实际名称。

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.

我的枚举定义如下:

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

我有一个类定义为:

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;


推荐答案

您是否尝试使用

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

当检索数据时只需解析它

And when retrieving data just Parse it

这篇关于如何在组合框中显示枚举值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆