Click here to Skip to main content
Click here to Skip to main content

Bind Enum data to Dropdown List By Sorting

By , 29 Dec 2010
 
    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)

About the Author

sat_nar
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 5 looking good please explain code -- ...memberPranay Rana30 Dec '10 - 16:47 
GeneralReason for my vote of 4 get 4 from me -- but include descrip...memberPranay Rana29 Dec '10 - 19:22 
GeneralReason for my vote of 1 Whats with people creating DataTable...memberSledgeHammer0129 Dec '10 - 9:32 
GeneralAnd I agree with Shahriar - you need to add some description...mvpJohn Simmons / outlaw programmer26 Dec '10 - 4:35 
GeneralAdded ASP.Net to the tags. Platform is as important as the l...mvpJohn Simmons / outlaw programmer26 Dec '10 - 4:22 
GeneralNeed bit details explanation what each function does.memberShahriar Iqbal Chowdhury25 Dec '10 - 20:34 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 29 Dec 2010
Article Copyright 2010 by sat_nar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid