I am currently have grid with captions and column names filled in from enumeration.
private enum enumCaptions
{
[Description("Status")]
Active,
[Description("Part number")]
Part,
[Description("Level")]
Change,
[Description("Project")]
Program,
[Description("Location")]
Location
}
Function that allowing me to read Description, based on value selected
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();
}
}
And method that doing just that, looping through my enumeration and assigning captions and names to columns
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++;
}
}
Since I have a few grids, but they all should have different data and captions, I would like to create generic function that will take any grid name, any enumeration and assign descriptions to grid captions. But part where I getting description is not working anymore, enum captionsEnumis not recognized as Enum anymore, however Name values returned properly.
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());
}
Any help will be greatly appreciated. Thank you.