Click here to Skip to main content
12,829,103 members (39,688 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

14.2K views
3 bookmarked
Posted 29 Dec 2010

Bind Enum data to Dropdown List By Sorting

, 29 Dec 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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();
        dtTemp.Columns.Add("ID");
        dtTemp.Columns.Add("Name");
        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];
            dtTemp.Rows.Add(drTemp);
        }
        DataView dvTemp = new DataView(dtTemp);
        dvTemp.Sort = "Name";
        dropDownList.DataTextField = "Name";
        dropDownList.DataValueField = "ID";
        dropDownList.DataSource = dvTemp;
        dropDownList.DataBind(); 
       
    }


Call Above Method

BindDropDownByEnum(ref DropDownList1, typeof(DignosisOrderType));

License

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

Share

About the Author

sat_nar
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralReason for my vote of 1 Whats with people creating DataTable... Pin
SledgeHammer0129-Dec-10 10:32
memberSledgeHammer0129-Dec-10 10:32 

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
Web01 | 2.8.170326.1 | Last Updated 29 Dec 2010
Article Copyright 2010 by sat_nar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid