Home > Asp.net > Using Enum in Gridview in Asp.net

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..

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: