如何在组合框中显示枚举值?

发布于 2024-10-18 03:55:56 字数 720 浏览 0 评论 0原文

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

我的枚举定义如下:

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;
    }
}

以及用于填充组合框的代码:

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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静水深流 2024-10-25 03:55:56

您是否尝试过使用

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

And 在检索数据时只是解析它

Have you tried to use

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

And when retrieving data just Parse it

┾廆蒐ゝ 2024-10-25 03:55:56

在这里我的代码,您可以将文本和值放在一起并填充组合框

 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";

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文