3

I am trying to bind a list of "Test" entities to the grid below. I am able to get the User column to read the Name property on Person class by specifying Person.Name for the datafield.

However, when I try to get the Desc column to display the Enum value's description by setting the datafield property to MyEnum.Description I get nothing. No errors, just a blank column.

Is this possible? Am I missing something? If I do this same kind of thing in the code-behind I would return a string of "Hello".

I would prefer to not use the ItemDataBound event in the code behind if it can be helped.

Public Class Test

    Property MyPerson As Person
    Property MyEnum As HelloWorldEnum = HelloWorldEnum.Hi

    Public Enum HelloWorldEnum
        <ComponentModel.Description("Hello")> Hi
        <ComponentModel.Description("World")> Earth
    End Enum

End Class

Public Class Person
    Property Name As String
End Class

A sample of my grid:

<telerik:radgrid id="grid" runat="server">
    <mastertableview>
        <Columns>
            <telerik:GridBoundColumn DataField="MyPerson.Name" HeaderText="User" />
            <telerik:GridBoundColumn DataField="MyEnum.Description" HeaderText="Desc" />
        </Columns>
    </mastertableview>
</telerik:radgrid>

Update: My apologies, I left out the fact that we have an extension method created to read the description attribute on the enum. Hence my use of .Description.

<Extension()>
Public Function Description(ByVal theEnum As [Enum]) As String
    Dim fi As FieldInfo = theEnum.GetType().GetField(theEnum.ToString)
    Dim attributes() As DescriptionAttribute = DirectCast(fi.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
    If attributes.Length > 0 Then
        Return attributes(0).Description
    Else
        Return theEnum.ToString
    End If
End Function
Airn5475
  • 2,452
  • 29
  • 51

1 Answers1

2

It is not about Telerik RadGrid. You need a helper method to get description of enum.

Adding Descriptions to your Enumerations

public static string GetDescription(object enumValue, string defDesc)
{
  FieldInfo fi = enumValue.GetType().GetField(enumValue.ToString());

  if (null != fi)
  {
     object[] attrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), true);
     if (attrs.Length > 0)
            return ((DescriptionAttribute)attrs[0]).Description;
  }
  return defDesc;
}

enter image description here

ASPX

<telerik:RadGrid ID="grid" runat="server" 
    OnNeedDataSource="grid_NeedDataSource" 
    AutoGenerateColumns="False">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="MyPerson.Name" 
                HeaderText="User" />
            <telerik:GridBoundColumn DataField="MyEnum" HeaderText="Value" />
            <telerik:GridTemplateColumn HeaderText="Desc" >
                <ItemTemplate>
                    <%# GetDescription(Eval("MyEnum"), "Nothing") %>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

ASPX.CS

public class Test
{

    public Person MyPerson { get; set; }
    public HelloWorldEnum MyEnum { get; set; }

    public enum HelloWorldEnum
    {
        [Description("Hello")]
        Hi,
        [Description("World")]
        Earth
    }
}

public class Person
{
    public string Name { get; set; }
}

protected void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    grid.DataSource = new List<Test>()
    {
        new Test
        {
            MyPerson = new Person {Name = "Jon"},
            MyEnum = Test.HelloWorldEnum.Earth
        }
    };
}
Win
  • 61,100
  • 13
  • 102
  • 181
  • I updated my question to include the extension method we are using to grab the Enum Description. Thank you for the idea of using the Tempate Column. That certainly is one route we could go. I am still looking for an answer as to why I can't call that extension method directly in the DataField property of a normal column. – Airn5475 Oct 18 '13 at 12:18
  • **Description** is an extension method. You cannot use like a property, although VB let you write **MyEnum.Description**. Correct syntax is **MyEnum.Description()**. ***Now we are back to orignanl question - we cannot call a method inside DataField like this - DataField="MyEnum.Description()"*** – Win Oct 18 '13 at 14:10