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 4342540
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:37:54+00:00 2026-05-21T11:37:54+00:00

I would bind the values of an enumeration with a combobox control. I’ve written

  • 0

I would bind the values of an enumeration with a combobox control.

I’ve written this code:

cboPriorLogicalOperator.DataSource = Enum.GetValues(typeof(MyEnum))
    .Cast<MyEnum>()
    .Select(p => new { Key = (int)p, Value = p.ToString() })
    .ToList();

myComboBox.DisplayMember = "Value";
myComboBox.ValueMember = "Key";

It works well but I wonder if there is a simpler way.

  • 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-21T11:37:55+00:00Added an answer on May 21, 2026 at 11:37 am

    I think your code is beautiful!

    The only improvement would be to place the code in an Extension Method.

    EDIT:

    When I think about it, what you want to do is to use the Enum as in the definition and not an instance of the enum, which is required by extensions methods.

    I found this question, which solves it really nicely:

    public class SelectList
    {
        // Normal SelectList properties/methods go here
    
        public static SelectList Of<T>()
        {
           Type t = typeof(T);
           if (t.IsEnum)
           {
               var values = from Enum e in Enum.GetValues(t)
                            select new { ID = e, Name = e.ToString() };
               return new SelectList(values, "Id", "Name");
           }
           return null;
        }
    }
    
    // called with 
    var list = SelectList.Of<Things>();
    

    Only you might want to return a Dictionary<int, string> and not a SelectList, but you get the idea.

    EDIT2:

    Here we go with a code example that covers the case you are looking at.

    public class EnumList
    {
        public static IEnumerable<KeyValuePair<T, string>> Of<T>()
        {
            return Enum.GetValues(typeof (T))
                .Cast<T>()
                .Select(p => new KeyValuePair<T, string>(p, p.ToString()))
                .ToList();
        }
    }
    

    Or this version perhaps, where the key is an int

    public class EnumList
    {
        public static IEnumerable<KeyValuePair<int, string>> Of<T>()
        {
            return Enum.GetValues(typeof (T))
                .Cast<T>()
                .Select(p => new KeyValuePair<int, string>(Convert.ToInt32(p), p.ToString()))
                .ToList();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a dictionary of <string, Drink> , how would you bind the dictionary.Values to
In the Erlang interactive shell you can bind variables to values. If I would
I would like to bind a TreeView control I have defined in XAML to
I would like to bind an Image to some kind of control an delete
I would like to bind C-` (control-backquote) but I could not do it. The
I've seen in WPF where you can bind control values to properties of other
I would like to know how i can bind my form values to my
I use a whitelist to bind querystring/form values to my model. This is done
I have a Silverlight (WP7) project and would like to bind an enum to
I am able to bind a MyDictionary.Value to a ComboBox and see the Values

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.