开发者

formatting enum in gridview

i need to display the name of enum in gridview by data table returns its numeric value

i am using this for other columns

<asp:BoundField DataField="Name" HeaderText="User Name" /> 

i nee开发者_JAVA百科d to use it for enum to display the string value of enum Gender

<asp:BoundField DataField="Gender" HeaderText="Gender" /> 


Try this solution

Enum.GetName Method

<asp:TemplateField HeaderText="Category">
<ItemTemplate>
<div>
<%# Enum.GetName(typeof(GlobalLibrary.Constants.Category),Convert.ToInt32(Eval("Category"))) %>
</div>
</ItemTemplate>
</asp:TemplateField>


It helped me :) And then I found this simpler

<asp:TemplateField HeaderText="Gender">
  <ItemTemplate><%#(MyGenderEnum)Eval("Gender")%></ItemTemplate>
</asp:TemplateField>


And if you prefer VB.NET:

 <asp:TemplateField HeaderText="Status" SortExpression="VisibilityStatus">
      <ItemTemplate>
          <%# [Enum].GetName(GetType(VisibilityStatusEnum), Eval("VisibilityStatus"))%>
      </ItemTemplate>
 </asp:TemplateField>


This version worked for me in VB.NET:

<asp:TemplateField HeaderText="Gender">
   <ItemTemplate><%# CType(Eval("Gender"), Gender).ToString() %></ItemTemplate>
</asp:TemplateField>

Iterestingly, it didn't work with DirectCast instead of CType, and was still displaying integers until I added ToString(). I also had to add namespace to my enum in my case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜