枚举类型的显示

xu6740 2020-09-19 11:01:22
定义 一个枚举类型 如下:
namespace Model.FileEnum
{
public enum SexEnum
{
[Description("未知")]
UnKnown = 0,
[Description("男")]
Man = 1,
[Description("女")]
Wonman = 2,
}
}
var sexs = new List<SexEnum>();
gridcontrol.datasource = sexs;
之后GridControl 里显示的是: UnKnow
如何让他显示 为 未知 即中文内容
...全文
4402 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xu6740 2020-09-20
  • 打赏
  • 举报
回复
感谢8楼 提供 关键字。问题已解决
xu6740 2020-09-20
  • 打赏
  • 举报
回复
我可能是没有问清楚哦!是这样 有个人事资料View 人事资料里 Class EEmployee { public int Id{get;set;} public string name {get;set;} public SexEnum Sex {get;set;} } var eEmployees = new List<EEmployee>(){1,"小强",SexEnum .Man}; gridControl.datasource = eEmployees ; 想达到的效果是:在这个Grid里显示 出来的是 男 而不是 Man 跪求 解决方案
wanghui0380 2020-09-20
  • 打赏
  • 举报
回复
C# datagridview CellFormatting 自己百度,5分钟问题,自己百度。不知道关键词我们可以给,但关键词都给了,自己都懒得百度,我们就帮不了。
data-v 2020-09-19
  • 打赏
  • 举报
回复
GetCustomAttribute 写中文就好了
闪耀星星 2020-09-19
  • 打赏
  • 举报
回复
写个帮助类 Dictionary<int,string> dic = new Dictionary<int,string>(); foreach (var e in Enum.GetValues(typeof(T))) { object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true); if (objArr != null && objArr.Length > 0) { DescriptionAttribute da = objArr[0] as DescriptionAttribute; dic[Convert.ToInt32(e)] = da.Description; } }
  • 打赏
  • 举报
回复
或者用更加文雅的 Linq 语法来写:
var sexDescs = (from x in sexs
let type = x.GetType()
let name = Enum.GetName(type, x)
let fieldInfo = type.GetField(name)
let desc = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute)) as DescriptionAttribute
select desc.Description
).ToList();
  • 打赏
  • 举报
回复
var sexDescs = sexs.Select(x => Attribute.GetCustomAttribute(x.GetType().GetField(Enum.GetName(x.GetType(), x)), typeof(DescriptionAttribute)))
.Cast<DescriptionAttribute>().Select(x => x.Description).ToList();
masanaka 2020-09-19
  • 打赏
  • 举报
回复
https://stackoverflow.com/questions/479410/enum-tostring-with-user-friendly-strings
xu6740 2020-09-19
  • 打赏
  • 举报
回复
引用 1 楼 WQR1994 的回复:
GetCustomAttribute 写中文就好了
我是小白,哪个地方写中文 ,还是哪里调用 GetCustomarrribute 方法?

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧