Enumeration value usage Summary

Source: Internet
Author: User

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 );

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.