Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 need to use it for enum to display the string value of enum Gender

<asp:BoundField DataField="Gender" HeaderText="Gender" /> 
like image 891
vakas Avatar asked Aug 31 '10 07:08

vakas


3 Answers

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>
like image 197
Pranay Rana Avatar answered Nov 20 '22 02:11

Pranay Rana


It helped me :) And then I found this simpler

<asp:TemplateField HeaderText="Gender">
  <ItemTemplate><%#(MyGenderEnum)Eval("Gender")%></ItemTemplate>
</asp:TemplateField>
like image 21
Aximili Avatar answered Nov 20 '22 01:11

Aximili


And if you prefer VB.NET:

 <asp:TemplateField HeaderText="Status" SortExpression="VisibilityStatus">
      <ItemTemplate>
          <%# [Enum].GetName(GetType(VisibilityStatusEnum), Eval("VisibilityStatus"))%>
      </ItemTemplate>
 </asp:TemplateField>
like image 1
Leigh Avatar answered Nov 20 '22 01:11

Leigh