Bind the enumerated values to the drop-down box:
Method 1:
Bind
Enum TestEnum {zero = 0, onE = 1, two = 2}
ComboBox cbo = new ComboBox ();
Cbo. DataSource = System. Enum. GetNames (typeof (TestEnum ));
Enum test = enum. onE;
Cbo. SelectedIndex = this. cbo. FindString (test. ToString ());
Value
TestEnum testenum = (TestEnum) Enum. Parse (typeof (TestEnum), cbo. SelectedItem. ToString (), false)
Method 2:
Foreach (var v in typeof (AA). GetFields ())
{
If (v. FieldType. IsEnum = true)
{
This. comboBox1.Items. Add (v. Name );
}
}
This. comboBox1.SelectedIndex = 1;
Method 3:
Reflection, enumeration, binding drop-down box
Public static class EnumManager <TEnum>
{
Private static DataTable GetDataTable ()
{
Type enumType = typeof (TEnum); // obtain the Type object
FieldInfo [] enumFields = enumType. GetFields (); // obtains a set of field information objects.
DataTable table = new DataTable ();
Table. Columns. Add ("Name", Type. GetType ("System. String "));
Table. Columns. Add ("Value", Type. GetType ("System. Int32 "));
// Traverse the set
Foreach (FieldInfo field in enumFields)
{
If (! Field. IsSpecialName)
{
DataRow row = table. NewRow ();
Row [0] = field. Name; // obtain the field text value
Row [1] = Convert. ToInt32 (field. GetRawConstantValue (); // obtain the int Value
// Row [1] = (int) Enum. Parse (enumType, field. Name );
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 {
Not submitted = 1,
Submitted,
Canceled,
Completed = 6
}
EnumManager <BookingStauts>. SetListControl (ddlBookingStatus );
EnumManager <TicketStatus>. SetListControl (rblTicketStatus );