Click here to Skip to main content
13,150,867 members (47,406 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 28 Jan 2014

Enum To Datatable

, 28 Jan 2014
Rate this:
Please Sign up or sign in to vote.
Enum to datatable

A Comprehensive Utility to Play with Enum

I am often asked by developers in my team to write a code that can convert the enums to a collection structure which will be useable as a data source to any ASP.NET and/or HTML control and would be exposeable by Web Services. However, Enums can also be used as a data source to any server side container controls, the problem is to use them in HTML controls with JSON format.

We have some utilities that can convert the numerous data structures to JSON format and DataTable is one of them. To utilize such structure in Restful Services, I decided to write a code that converts any Enum Type to DataTable. It is a small but comprehensive utility which makes the developer's life easy. Hope you like it.

Enum to DataTable Convertor Function

public DataTableEnumToDataTable(Type enumType)
            DataTable table = newDataTable();
            //Column that contains the Captions/Keys of Enum        
            table.Columns.Add("Desc", typeof(string));
            //Get the type of ENUM for DataColumn
            table.Columns.Add("Id", Enum.GetUnderlyingType(enumType));
            //Add the items from the enum:
            foreach (string name in Enum.GetNames(enumType))
                //Replace underscores with space from caption/key and add item to collection:
                table.Rows.Add(name.Replace('_', ' '), Enum.Parse(enumType, name));
            return table;

Method Calling

DataTable dT = EnumToDataTable(typeof(ENUM_CURRENCY));


I love to write such tiny utilities which enable my team to smoothly fulfill the task in a timely manner. I always enjoy writing such utilities because these make my work enjoyable to me.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kashif Akhter
Team Leader
Pakistan Pakistan
A computer software development professional with a proven track record of extensive experience of enterprise software development and building the manageable, scalable, and robust enterprise software architectures.

You may also be interested in...

Comments and Discussions

GeneralThank you Pin
pAtuAry14-Mar-15 1:25
memberpAtuAry14-Mar-15 1:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170924.2 | Last Updated 28 Jan 2014
Article Copyright 2014 by Kashif Akhter
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid