gpt4 book ai didi

datagridview - DataGridView中的枚举描述

转载 作者:行者123 更新时间:2023-12-02 02:19:50 24 4
gpt4 key购买 nike

我有一个枚举说明问题。
我希望dataGrid向我显示枚举说明,而不是枚举的“ToString()”。

enum DirectionEnum
{
[Description("Right to left")]
rtl,

[Description("Left to right")]
ltr
}
class Simple
{
[DisplayName("Name")]
public string Name { get; set; }

[DisplayName("Direction")]
public DirectionEnum dir { get; set; }
}
class DirectionDialog : Form
{
public DirectionDialog()
{
DataGridView table = new DataGridView();
List<Simple> list = new List<Simple>(new Simple[]{
new Simple{ Name = "dave", dir = DirectionEnum.ltr},
new Simple{ Name = "dan", dir = DirectionEnum.rtl }
});
table.DataSource = list;
//view "rtl" or "ltr" in "Direction"
//I want "Right to left" or "Left to right:
}
}

我想通过枚举的说明查看“方向”列。
我在做什么
对不起,我的英语不好。

最佳答案

class Simple
{
[DisplayName("Name")]
public string Name { get; set; }

// Remove external access to the enum value
public DirectionEnum dir { private get; set; }

// Add a new string property for the description
[DisplayName("Direction")]
public string DirDesc
{
get
{
System.Reflection.FieldInfo field = dir.GetType().GetField(dir.ToString());

DescriptionAttribute attribute
= Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
as DescriptionAttribute;

return attribute == null ? dir.ToString() : attribute.Description;
}
}
}

关于datagridview - DataGridView中的枚举描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8825154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com