How do you change an enum value to a more readable string that is bound to a UltraGrid?

467 Views Asked by At

I have an Infragistics UltraGrid with a bound list of various datatypes. One of these is an enum with non-human readable values. I would like to change the display to something more readable.

In the code base people have been hiding the enum column and adding a string column with the desired values. This doesn't seem right to me. Is there a way of changing the displayed enum values that would be more readable?

Example. Not Readable:
SomeUnreadableEnumValue
Some_Unreadable_Enum_Value

Readable:
Some Readable Text

Update:
I am aware of using Description attributes

public enum MyEnum
{
    [Description("Description for Foo")]
    Foo,
    [Description("Description for Bar")]
    Bar
}

as mentioned here See Thomas Levesque answer. I just can't figure out how to bind this description attribute to an UltraGrid that is already bound to an enum.

1

There are 1 best solutions below

0
On

I just create a property that returns the description and bind to it

public override string UserTextOp
{
    get
    {
        Type enumType = typeof(enumTextCond);
        string name = Enum.GetName(enumType, cond1SelectedKeyEnum);
        if (name != null)
        {
            FieldInfo field = enumType.GetField(name);
            if (field != null)
            {
                DescriptionAttribute attr =
                        Attribute.GetCustomAttribute(field,
                            typeof(DescriptionAttribute)) as DescriptionAttribute;
                if (attr != null)
                    name = attr.Description;
            }
            return name;
        }
        return string.Empty;

    }
}