0
我目前擁有從枚舉中填充字幕和列名的網格。重新枚舉枚舉
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++;
}
}
因爲我有幾個網格,但他們都應該有不同的數據和標題,我想創建泛型函數,將採取任何網格名稱,任何枚舉和將描述分配給網格標題。 但是我得到描述的部分不再工作了,枚舉字幕Enum不再被識別爲Enum,但Name值正確返回。
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());
}
任何幫助將不勝感激。 謝謝。
這做什麼他原來fillGridCaptions一樣。我認爲gridCaptions的意義在於使他能夠傳遞他想要在列中使用的枚舉標誌。 – MisterXero 2013-05-01 18:38:23
@MisterXero:我將一些參數重命名爲更通用的名稱。注意:這些只是名稱,不是數據類型。現在它給我的答案實際上做了更好的印象。 – 2013-05-01 18:42:17
他的原始方法gridCaptions不是做同樣的事情嗎? – MisterXero 2013-05-01 18:46:54