Using a Windows Forms Combo Window with a list of <KeyValuePair <UserEnum, String >> as a DataSource - C #
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 DisplayMembers and use a list of user-defined enum values as ValueMember. I am currently returning List> from my database access function, and I would like to bind it to my Combo field. I tried to assign the list to the .DataSource property by assigning "Key" .DataMember and "Value" to .DisplayMember. This is clearly an invalid approach as it does not work.
Can someone please give me a different approach which is in good shape and actually working?
thanks
I use my own EnumPair<> class in combination with two extension methods to bind property lists to properties with enumerated types.
See if this helps you, that you can work directly with transfers.
Use it as follows:
comboBox.BindToEnumValue<MyEnumType>(myBindingSourceInstance, "PropertyNameOfBindingSource"); It is assumed that you have a ComboBox named "comboBox" in your form, an Enum called "MyEnumType", and an instance of BindingSource. The PropertyNameOfBindingSource property must be the name of a property of a type that has a BindingSource list that has a PropertyType MyEnumType. The implementation for background work is given below, extension methods are not needed, I just do not like to write almost 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 }