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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:32:02+00:00 2026-05-13T08:32:02+00:00

I’m currently working on a Windows Forms GUI and I have a Combo that

  • 0

I’m currently working on a Windows Forms GUI and I have a Combo that I need to display a list of string values as the DisplayMembers and use a list of user defined enum values as the ValueMember. I’m currently returning a List> from my database access function and I would like to bind this to my Combo box. I’ve tried assigning the list to the .DataSource property, assigning “Key” to .DataMember and “Value” to .DisplayMember. This is clearly not a valid approach as it is not working.

Can someone please give me another approach that is in good form and actually works?

Thanks

  • 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-13T08:32:03+00:00Added an answer on May 13, 2026 at 8:32 am

    I do use my own class EnumPair<> in combination with two extension methods to bind comboboxes to Properties with enum types.

    See if this can help you, that you can work directly with the enums.

    Use it like this after implementation:

    comboBox.BindToEnumValue<MyEnumType>(myBindingSourceInstance, "PropertyNameOfBindingSource");
    

    That assumes you have a ComboBox named “comboBox” on your form, an Enum called “MyEnumType” and an instance of a BindingSource. The PropertyNameOfBindingSource should be the name of the Property of the type that your BindingSource has a list of, that has the PropertyType of MyEnumType.
    Implementation for the background work is found below, the extension methods are not needed, i just do not like write nearly identical lines of code 😉

    public static class ComboBoxExtensions
    {
        public static void BindToEnumValue<TEnum>(this ComboBox cbo, BindingSource bs, string propertyName)
        {
            cbo.DataSource = EnumPair<TEnum>.GetValuePairList();
            cbo.ValueMember = EnumPair<TEnum>.ValueMember;
            cbo.DisplayMember = EnumPair<TEnum>.DisplayMember;
            cbo.DataBindings.Add(new Binding("SelectedValue", bs, propertyName));
        }
    
        public static void BindClear(this ComboBox cbo)
        {
            cbo.DataSource = null;
            cbo.DataBindings.Clear();
        }
    }
    
    /// <summary>
    /// Represents a <see cref="EnumPair"/> consisting of an value 
    /// of an enum T and a string represantion of the value.
    /// </summary>
    /// <remarks>
    /// With this generic class every <see cref="Enum"/> can be
    /// dynamically enhanced by additional values, such as an empty
    /// entry, which is usefull in beeing used with 
    /// <see cref="ComboBox"/>es.
    /// </remarks>
    /// <typeparam name="T">The type of the <see cref="Enum"/> to represent.</typeparam>
    public partial class EnumPair<T>
    {
        #region Constants
    
        public const string ValueMember = "EnumValue";
        public const string DisplayMember = "EnumStringValue";
    
        #endregion
    
        #region Constructor
    
        /// <summary>
        /// Initializes a new instance of the <see cref="EnumPair"/> class.
        /// </summary>
        public EnumPair()
        {
            Type t = typeof(T);
            if (!t.IsEnum)
            {
                throw new ArgumentException("Class EnumPair<T> can only be instantiated with Enum-Types!");
            }
        }
    
        /// <summary>
        /// Initializes a new instance of the <see cref="EnumPair"/> class.
        /// </summary>
        /// <param name="value">The value of the enum.</param>
        /// <param name="stringValue">The <see cref="string"/> value of the enum.</param>
        public EnumPair(T value, string stringValue)
        {
            Type t = typeof(T);
            if (!t.IsEnum)
            {
                throw new ArgumentException("Class EnumPair<T> can only be instantiated with Enum-Types!");
            }
    
            this.EnumValue = value;
            this.EnumStringValue = stringValue;
        }
    
        #endregion
    
        #region Properties
    
        /// <summary>
        /// Gets or sets the value part of the <see cref="EnumPair"/>.
        /// </summary>
        public T EnumValue { get; set; }
    
        /// <summary>
        /// Gets or sets the string value of the <see cref="EnumPair"/>.
        /// </summary>
        public string EnumStringValue { get; set; }
    
        #endregion
    
        #region Methods
    
        /// <summary>
        /// Returns a <see cref="string"/> that represents the current <see cref="EnumPair"/>.
        /// </summary>
        public override string ToString()
        {
            return this.EnumStringValue;
        }
    
        /// <summary>
        /// Generates a <see cref="List<T>"/> of the values
        /// of the <see cref="Enum"/> T.
        /// </summary>
        public static List<EnumPair<T>> GetValuePairList()
        {
            List<EnumPair<T>> list = new List<EnumPair<T>>();
            EnumPair<T> pair = new EnumPair<T>();
    
            foreach (var item in Enum.GetValues(typeof(T)))
            {
                pair = new EnumPair<T>();
                pair.EnumValue = (T)item;
                pair.EnumStringValue = ((T)item).ToString();
                list.Add(pair);
            }
    
            return list;
        }
    
        /// <summary>
        /// Implicit conversion from enum value to <see cref="EnumPair<>"/> from that enum.
        /// </summary>
        /// <param name="e">The enum value to convert to.</param>
        /// <returns>A <see cref="EnumPair<>"/> to the enum value.</returns>
        public static implicit operator EnumPair<T>(T e)
        {
            Type t = typeof(EnumPair<>).MakeGenericType(e.GetType());
            return new EnumPair<T>((T)e, ((T)e).ToString());
        }
    
        #endregion
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I'm making a simple page using Google Maps API 3. My first. One marker

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.