Databinding a .Net WinForms ComboBox to an Enum
Posted
by Tim Huffam
on Geeks with Blogs
See other posts from Geeks with Blogs
or by Tim Huffam
Published on Thu, 08 Apr 2010 03:00:19 GMT
Indexed on
2010/04/08
3:03 UTC
Read the original article
Hit count: 284
This is quite simple...
Define the enum eg:
public enum MyEnum{ ItemOne, ItemTwo, }
Within the form set the datasource of the combobox to the values of the enum eg:
myCombo.DataSource = System.Enum.GetValues(typeof(MyEnum));
To have the combo auto select a value based on a bound object, set the databinding of the combo eg:
class MyObject{ private MyEnum myEnumProperty; public MyEnum MyEnumProperty{get {return myEnumProperty;}} } MyObject myObj = new MyObject(); myCombo.DataBindings.Add(new Binding("SelectedIndex", myObject, "MyEnumProperty");
© Geeks with Blogs or respective owner