From: http://blog.csdn.net/crazy_frog/article/details/7705442
Method One:
Binding
[CSharp]View Plaincopy
- Enum Testenum {zero=0,one=1,two=2}
- ComboBox CBO = new ComboBox ();
- Cbo. DataSource = System.Enum.GetNames (typeof (Testenum));
- Testenum test = testenum. One;
- Cbo. SelectedIndex = this.cbo.FindString (test. ToString ());
- Take value
- Testenum testenum = (testenum) enum.parse (typeof (Testenum), CBO. Selecteditem.tostring (),false)
Method Two:
[CSharp]View Plaincopy
- foreach (Var v in typeof (AA). GetFields ())
- {
- if (V.fieldtype.isenum = = true)
- {
- This.comboBox1.Items.Add (v.name);
- }
- }
- This.comboBox1.SelectedIndex = 1;
Method Three:
Reflection, enumeration, binding drop Box
[CSharp]View Plaincopy
- Public static class enummanager<tenum>
- {
- private static DataTable getdatatable ()
- {
- Type enumtype = typeof (Tenum); //Get type Object
- fieldinfo[] EnumFields = Enumtype.getfields (); //Get a collection of field information objects
- DataTable table = new DataTable ();
- Table. Columns.Add ("Name", Type.GetType ("System.String"));
- Table. Columns.Add ("Value", Type.GetType ("System.Int32"));
- //Traversal collection
- foreach (FieldInfo field in enumfields)
- {
- if (!field. Isspecialname)
- {
- DataRow row = table. NewRow ();
- Row[0] = field. Name; //Get field text value
- ROW[1] = convert.toint32 (field. Getrawconstantvalue ()); //Get int value
- //row[1] = (int) enum.parse (enumtype, field. Name); You can do that .
- Table. Rows.Add (row);
- }
- }
- return table;
- }
- public static void Setlistcontrol (ListControl list)
- {
- List. DataSource = Getdatatable ();
- List. DataTextField = "Name";
- List. DataValueField = "Value";
- List. DataBind ();
- }
- }
- Public enum Bookingstatus {
- Uncommitted = 1,
- has been submitted,
- has been canceled,
- Completed = 6
- }
- Enummanager<bookingstauts>. Setlistcontrol (Ddlbookingstatus);
- Enummanager<ticketstatus>. Setlistcontrol (Rblticketstatus);
C # enumeration bound to ComboBox