Let's say that I have following enum
public enum MyMode { A = 1, B = 2, C = 3, D = 4 };
and I want to use this enum as list of values inside combobox, I tried with
cmbMyMode.Items.Add(Enum.GetValues(typeof(MyMode )));
but I'm getting following
MyMode[] Array
I need to display A, B, C, D, and is it possible to use custom string instead of A,B,C,D
Thanks
List<MyMode> modes = Enum.GetValues(typeof(MyMode)).Cast<MyMode>().ToList();
cmbMyMode.DataSource = modes;
And to customize the labels:
var modes = Enum.GetValues(typeof(MyMode)).Cast<MyMode>().Select(mode =>
new { Value = mode, Title = string.Format("-->{0}<--", mode) }).
ToList();
cmbMyMode.ValueMember = "Value";
cmbMyMode.DisplayMember = "Title";
cmbMyMode.DataSource = modes;
and then
cmbMyMode.SelectedValue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With