vakas
vakas

Reputation: 1817

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" /> 

Upvotes: 6

Views: 7836

Answers (4)

Ekus
Ekus

Reputation: 1880

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.

Upvotes: 0

Leigh
Leigh

Reputation: 71

And if you prefer VB.NET:

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

Upvotes: 1

Pranay Rana
Pranay Rana

Reputation: 176936

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>

Upvotes: 8

Aximili
Aximili

Reputation: 29484

It helped me :) And then I found this simpler

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

Upvotes: 1

Related Questions