Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public enum States
   {
      

       [Description("New Mexico")]
       NewMexico = 1,

       [Description("New York")]
       NewYork = 2,

       [Description("South Carolina")]
       SouthCarolina = 3
   }



I am able to bind this but in value and in text only text is showing.
So i have to bind text and value both but text should contain space like "New Mexico"
Posted
Comments
Er. Puneet Goel 5-Apr-14 3:06am    
so whats the problem ?

 
Share this answer
 
C#
public static List<ListItem> LoadListForListControls<T>()
        {
            var type = typeof(T);
            var list = new List<ListItem>();
            foreach (var value in Enum.GetValues(type))
            {
                var enumValue = (Int32)value;
                var fi = value.GetType().GetField(value.ToString());
                var customAttributes =
                            fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                DescriptionAttribute attribute = null;
                if (customAttributes.Length > 0)
                    attribute = (DescriptionAttribute)customAttributes[0];
                var item = new ListItem
                {
                    Text = attribute != null
                               ? attribute.Description
                               : Enum.GetName(type, enumValue),
                    Value = (Convert.ToInt32(enumValue)).ToString()
                };

                list.Add(item);
            }

            return list;
        }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900