【发布时间】:2013-05-01 17:08:12
【问题描述】:
我目前有一个网格,其中包含从枚举中填充的标题和列名。
private enum enumCaptions
{
[Description("Status")]
Active,
[Description("Part number")]
Part,
[Description("Level")]
Change,
[Description("Project")]
Program,
[Description("Location")]
Location
}
允许我根据所选值阅读描述的功能
public class classStatics
{
public static string GetEnumDescription(Enum value)
{
//method to read description of the enumeration
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
}
这样做的方法是循环遍历我的枚举并将标题和名称分配给列
private void fillGridCaptions()
{
int i = 1;
grdMy.Cols.Count = Enum.GetValues(typeof(enumCaptions)).Length + 1;
foreach (int value in Enum.GetValues(typeof(enumCaptions)))
{
//assign name for every column from enum
grdMy.Cols[i].Name = Enum.GetName(typeof(enumCaptions), value);
//assign caption for every column
grdMy[0, i] = classStatics.GetEnumDescription((enumCaptions)value).ToString();
i++;
}
}
由于我有几个网格,但它们都应该有不同的数据和标题,我想创建一个通用函数,它将采用任何网格名称、任何枚举并将描述分配给网格标题。 但是我得到描述的部分不再起作用,枚举标题Enumis不再被识别为枚举,但是名称值正确返回。
private void gridCaptions(FlexGrid gridName, Enum captionsEnum)
{
int i = 1;
gridName.Cols.Count = Enum.GetValues(captionsEnum.GetType()).Length + 1;
foreach (int value in Enum.GetValues(captionsEnum.GetType()))
{
gridName.Cols[i].Name = Enum.GetName(captionsEnum.GetType(), value);
//assign caption for every column
gridName[0, i] = classStatics.GetEnumDescription(captionsEnum);
i++;
}
}
private void fillGridCaptions()
{
gridCaptionsgrdMy, new enumCaptions());
}
任何帮助将不胜感激。 谢谢。
【问题讨论】: