How to associate an enumeration with a list

I would bind enum values ​​using a combobox control.

I wrote 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"; 

This works well, but I wonder if there is an easier way.

+39
c # winforms
Apr 12 '11 at 16:33
source share
5 answers

I think your code is beautiful!

The only improvement will be the placement of code in the extension method.

EDIT:

When I think about this, you want to use Enum as in the definition, and not as an instance of enum, which is required for extension methods.

I found this question that solves it very well:

 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 are a SelectList idea.

EDIT2:

Here we give an example of code 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, possibly where the key is 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(); } } 
+25
Apr 12 2018-11-11T00:
source share

Why not use:

 myComboBox.DataSource = Enum.GetValues(typeof(MyEnum)) 

?

+6
Apr 14 '11 at 8:45
source share
 foreach (int r in Enum.GetValues(typeof(MyEnum))) { var item = new ListItem(Enum.GetName(typeof(MyEnum), r), r.ToString()); ddl.Items.Add(item); } 
+1
Jun 27 '13 at 22:23
source share

I recently ran into a problem when I had the nullum enum property and needed to associate it with a ComboBox. Here is the solution I came up with:

 using System; using System.Collections.Generic; namespace ActivitySchedule.Model { public class NullableEnum<T> where T : struct, IComparable { public string Display { get; private set; } public T? Value { get; private set; } public static implicit operator T?(NullableEnum<T> o) { return o.Value; } public static implicit operator NullableEnum<T>(T? o) { return new NullableEnum<T> { Display = o?.ToString() ?? "NA", Value = o }; } private NullableEnum() { } public static IEnumerable<NullableEnum<T>> GetList() { var items = new List<NullableEnum<T>> { new NullableEnum<T> { Display = "NA", Value = null } }; var values = Enum.GetValues(typeof(T)); foreach (T v in values) { items.Add(v); } return items; } } } 

I wrapped the object in the Controller class and changed the Type property as follows:

 private MyClass myClass; public NullableEnum<MyEnum> MyEnum { get { return this.myClass.MyEnum; } set { this.myClass.MyEnum = value.Value; } } 

(it can also be a derived class and override a property)

Here is how I used it:

 var types = NullableEnum<MyEnum>.GetList(); this.comboBox1.DataSource = types; this.comboBox1.DisplayMember = "Display"; this.comboBox1.ValueMember = "Value"; this.comboBox1.Bindings.Add("SelectedValue", myClassController, "MyEnum"); 
0
Jan 22 '16 at 15:48
source share
 private void Form1_Load(object sender, EventArgs e) { comboBox1.DataSource = Enum.GetValues( typeof(Gender)); Array gen = Enum.GetValues(typeof(Gender)); List<KeyValuePair<string, char>> lstgender = new List<KeyValuePair<string,char>>(); foreach(Gender g in gen) lstgender.Add(new KeyValuePair<string,char>(g.ToString(),((char)g))); comboBox1.DataSource = lstgender; comboBox1.DisplayMember = "key"; comboBox1.ValueMember = "value" } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(comboBox1.SelectedValue.ToString()); } public class Student { public string stud_name { get; set; } public Gender stud_gen { get; set; } } public enum Gender { Male='M', Female='F' } 
-one
Feb 23 '17 at 14:01
source share



All Articles