来自:http://blog.csdn.net/crazy_frog/article/details/7705442
方法一:
绑定
[csharp] view plaincopy
class="dp-c" start="1">
- 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());
- 取值
- TestEnum testenum = (TestEnum)Enum.Parse(typeof(TestEnum) ,cbo.SelectedItem.ToString() ,false)
方法二:
[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;
方法三:
反射,枚举,绑定下拉框
[csharp] view plaincopy
- public static class EnumManager<TEnum>
- {
- private static DataTable GetDataTable()
- {
- Type enumType = typeof(TEnum);
- FieldInfo[] enumFields = enumType.GetFields();
- DataTable table = new DataTable();
- table.Columns.Add("Name", Type.GetType("System.String"));
- table.Columns.Add("Value", Type.GetType("System.Int32"));
-
- foreach (FieldInfo field in enumFields)
- {
- if (!field.IsSpecialName)
- {
- DataRow row = table.NewRow();
- row[0] = field.Name;
- row[1] = Convert.ToInt32(field.GetRawConstantValue());
-
- 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 {
- 未提交 = 1,
- 已提交,
- 已取消,
- 已完成 = 6
- }
- EnumManager<BookingStauts>.SetListControl(ddlBookingStatus);
- EnumManager<TicketStatus>.SetListControl(rblTicketStatus);