Tuesday, December 20, 2011

Using Enum in Gridview in Asp.net


Lets say we want to bind a gridview with table having Fields
1) Customer Name
2) Category
Now in the database Category field is stored as tiny int like 0 or 1 or 2
0 Stands for Sales
1 Stands for Marketing
2 Stands for Production
So, now when the gridview is bind, it will display o,1 values. instead of this we want their respective category name to be displayed.
For this we define a ENUM as follows
public enum Category
{
Sales= 0,
Marketing= 1,
Production= 2
}
Now in the gridview source in the template field write this code.
<asp:TemplateField HeaderText=”Category”>
<ItemTemplate>
<div>
<%# Enum.GetName(typeof(GlobalLibrary.Constants.Category),Convert.ToInt32(Eval(“Category”))) %>
</div>
</ItemTemplate>
</asp:TemplateField>
Here in Globallibray.Constants is my project with Constants Class.. you can replace it with yours..

No comments :

Post a Comment