0

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.

4

1 回答 1

0

如果稍微修改了您的代码。我修改了以下几行:

  • 在 for 循环中,从value更改intobject,存储的不是数字,而是枚举中的实际项目。
  • anyGrid.Cols[i].Name 现在直接分配了枚举项的字符串表示形式。
  • anyGrid[0, i] 仍然分配有描述。由于强制转换为 ,可以调用该函数Enum。当它是 时这是不可能的int,因为枚举的类型在一般情况下是未知的。

编码:

private void gridCaptions(FlexGrid anyGrid, Enum anyEnum)
{
        int i = 1;
        anyGrid.Cols.Count = Enum.GetValues(anyEnum.GetType()).Length + 1;
        foreach (object value in Enum.GetValues(anyEnum.GetType()))
        {
            anyGrid.Cols[i].Name = value.ToString();
            anyGrid[0, i] = classStatics.GetEnumDescription((Enum)value);
            i++;
        }
}
于 2013-05-01T18:34:18.850 回答