Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6236561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:52:24+00:00 2026-05-24T10:52:24+00:00

I have an enum like this: public enum Cities { [Description(New York City)] NewYork,

  • 0

I have an enum like this:

public enum Cities
{
    [Description("New York City")]
    NewYork,
    [Description("Los Angeles")]
    LosAngeles,
    Washington,
    [Description("San Antonio")]
    SanAntonio,
    Chicago
}

I want to bind this to a combobox and I’ve tried this:

comboBox.DataSource = Enum.GetNames(typeof(Cities));

But that displays the values in the combobox rather than the String description. So I switched to this:

public static string GetEnumDescription(Enum value)
{
    FieldInfo fi = value.GetType().GetField(value.ToString());

    DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

    if (attributes != null && attributes.Length > 0)
    {
        return attributes[0].Description;
    }
    else
    {
        return value.ToString();
    }
}

public static IList ToList(this Type type)
{
    ArrayList list = new ArrayList();
    Array enumValues = Enum.GetValues(type);

    foreach (Enum value in enumValues)
    {
        list.Add(new KeyValuePair<Enum, string>(value, GetEnumDescription(value)));
    }

    return list;
}

Now the list.Add() call results in the value and it’s string description being displayed in the combobox so I replaced

list.Add(new KeyValuePair<Enum, string>(value, GetEnumDescription(value)));

with

list.Add(GetEnumDescription(value));

and now I’m getting just the descriptive string displayed in the combobox which is what I ultimately want. Now my data binding is broken because it can’t find just the string description in the enumeration. I thought this might be related to combobox.DisplayMember and combobox.ValueMember but I haven’t been able to resolve the problem yet. Can anyone tell me how the heck I display the descriptive string but have my data binding use the value for storing, etc.? Thank you!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:52:25+00:00Added an answer on May 24, 2026 at 10:52 am

    Let’s go back to your question I answered a few days ago and modify that to suit your new requirements. So I’ll keep the colorEnum example in place of your Cities enum in this question.

    You’re most of the way there – you’ve got the code to go from the enum to the description string; now you just need to go back the other way.

    public static class EnumHelper
    {
        // your enum->string method (I just decluttered it a bit :))
        public static string GetEnumDescription(Enum value)
        {
            var fi = value.GetType().GetField(value.ToString());
            var attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
    
            if (attributes.Length > 0)
                return ((DescriptionAttribute)attributes[0]).Description;
            else
                return value.ToString();        
        }
    
        // the method to go from string->enum
        public static T GetEnumFromDescription<T>(string stringValue)
            where T : struct
        {
            foreach (object e in Enum.GetValues(typeof(T)))           
                if (GetEnumDescription((Enum)e).Equals(stringValue))
                    return (T)e;
            throw new ArgumentException("No matching enum value found.");
        }
    
        // and a method to get a list of string values - no KeyValuePair needed
        public static IEnumerable<string> GetEnumDescriptions(Type enumType)
        {
            var strings = new Collection<string>();
            foreach (Enum e in Enum.GetValues(enumType))   
                strings.Add(GetEnumDescription(e));
            return strings;
        }
    }
    

    Now, take what you had a few days ago…

    public class Person 
    {
        [...]
        public colorEnum FavoriteColor { get; set; }
        public string FavoriteColorString
        {
            get { return FavoriteColor.ToString(); }
            set { FavoriteColor = (colorEnum)Enum.Parse(typeof(colorEnum), value);  }
        }
    }
    

    and just change it to this:

    public class Person 
    {
        [...]
        public colorEnum FavoriteColor { get; set; }
        public string FavoriteColorString
        {
            get { return EnumHelper.GetEnumDescription(FavoriteColor); }
            set { FavoriteColor = EnumHelper.GetEnumFromDescription<colorEnum>(value); }
        }
    }
    

    As before, you’ll bind the combobox SelectedItem value to FavoriteColorString. You don’t need to set the DisplayMember or ValueMember properties if you’re still using the BindingSource as you were in the other question, which I assume you are.

    And change the combobox populating code from:

    comboBoxFavoriteColor.DataSource = Enum.GetNames(typeof(colorEnum));
    

    to

    comboBoxFavoriteColor.DataSource = EnumHelper.GetEnumDescriptions(typeof(colorEnum));
    

    Now you have the best of all worlds. The user sees the description, your code contains the enum names, and the data store contains the enum values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an enum with Description attributes like this: public enum MyEnum { Name1
I have a c# enumeration that looks like this: public enum EthernetLinkSpeed { [Description(10BASE-T)]
I have enum like this: public enum ObectTypes { TypeOne, TypeTwo, TypeThree, ... TypeTwenty
I have enum like this [Flags] public enum Key { None = 0, A
If I have an enum like this public enum Hungry { Somewhat, Very, CouldEatMySocks
Let's say you have an enum like this: public enum ColorsEnum { Undefined, Blue,
I have a class that defines its own enum like this: public class Test
I have an enum whose code is like this - public enum COSOptionType {
I have an enum type like this as an example: public Enum MyEnum {
I have defined my Enums like this. public enum UserType { RESELLER(Reseller), SERVICE_MANAGER(Manager), HOST(Host);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.