Click here to Skip to main content
15,123,695 members
Articles / Web Development / ASP.NET
Posted 29 Dec 2010

Tagged as


3 bookmarked

Bind Enum data to Dropdown List By Sorting

Rate me:
Please Sign up or sign in to vote.
4.43/5 (4 votes)
29 Dec 2010CPOL
public enum DignosisOrderType
    All = 0,
    General = 1,
    Uveitis = 2,
    Coag = 3,
    PreOp = 4,
    Tests = 5,
    RP = 6

public static void BindDropDownByEnum(ref DropDownList dropDownList, Type enumDataSource)
    DataTable dtTemp = new DataTable();
    string[] names = Enum.GetNames(enumDataSource);
    Array values = Enum.GetValues(enumDataSource);
    for (int i = 0; i < names.Length; i++)
        DataRow drTemp = dtTemp.NewRow();
        drTemp["ID"] = Convert.ToInt32((DignosisOrderType)Enum.Parse(typeof(DignosisOrderType), names[i])).ToString();
        drTemp["Name"] = names[i];
    DataView dvTemp = new DataView(dtTemp);
    dvTemp.Sort = "Name";
    dropDownList.DataTextField = "Name";
    dropDownList.DataValueField = "ID";
    dropDownList.DataSource = dvTemp;


Call Above Method

BindDropDownByEnum(ref DropDownList1, typeof(DignosisOrderType));


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


About the Author

United States United States
No Biography provided

Comments and Discussions

GeneralReason for my vote of 5 looking good please explain code -- ... Pin
Pranay Rana30-Dec-10 17:47
professionalPranay Rana30-Dec-10 17:47 
GeneralReason for my vote of 4 get 4 from me -- but include descrip... Pin
Pranay Rana29-Dec-10 20:22
professionalPranay Rana29-Dec-10 20:22 
GeneralReason for my vote of 1 Whats with people creating DataTable... Pin
SledgeHammer0129-Dec-10 10:32
MemberSledgeHammer0129-Dec-10 10:32 
GeneralAnd I agree with Shahriar - you need to add some description... Pin
#realJSOP26-Dec-10 5:35
mva#realJSOP26-Dec-10 5:35 
GeneralAdded ASP.Net to the tags. Platform is as important as the l... Pin
#realJSOP26-Dec-10 5:22
mva#realJSOP26-Dec-10 5:22 
GeneralNeed bit details explanation what each function does. Pin
Shahriar Iqbal Chowdhury/Galib25-Dec-10 21:34
professionalShahriar Iqbal Chowdhury/Galib25-Dec-10 21:34 

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.