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