我正在尝试将“测试”实体列表绑定到下面的网格。通过指定数据字段,我可以让 User 列读取 Person 类的 Name 属性Person.Name
。
但是,当我尝试通过将 datafield 属性设置为来让 Desc 列显示 Enum 值的描述时,MyEnum.Description
我什么也得不到。没有错误,只是一个空白列。
这可能吗?我错过了什么吗?如果我在代码隐藏中做同样的事情,我会返回一个“Hello”字符串。
如果可以提供帮助,我宁愿不要在后面的代码中使用 ItemDataBound 事件。
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
我的网格示例:
<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>
更新:抱歉,我忽略了我们创建了一个扩展方法来读取枚举上的描述属性的事实。因此我使用.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