C # enumeration bound to ComboBox

Source: Internet
Author: User

From: http://blog.csdn.net/crazy_frog/article/details/7705442

Method One:

Binding

[CSharp]View Plaincopy
    1. Enum Testenum {zero=0,one=1,two=2}
    2. ComboBox CBO = new ComboBox ();
    3. Cbo.  DataSource = System.Enum.GetNames (typeof (Testenum));
    4. Testenum test = testenum. One;
    5. Cbo. SelectedIndex = this.cbo.FindString (test.  ToString ());
    6. Take value
    7. Testenum testenum = (testenum) enum.parse (typeof (Testenum), CBO. Selecteditem.tostring (),false)



Method Two:

[CSharp]View Plaincopy
    1. foreach (Var v in typeof (AA). GetFields ())
    2. {
    3. if (V.fieldtype.isenum = = true)
    4. {
    5. This.comboBox1.Items.Add (v.name);
    6. }
    7. }
    8. This.comboBox1.SelectedIndex = 1;



Method Three:

Reflection, enumeration, binding drop Box

[CSharp]View Plaincopy
  1. Public static class enummanager<tenum>
  2. {
  3. private static DataTable getdatatable ()
  4. {
  5. Type enumtype = typeof (Tenum); //Get type Object
  6. fieldinfo[] EnumFields = Enumtype.getfields (); //Get a collection of field information objects
  7. DataTable table = new DataTable ();
  8. Table.  Columns.Add ("Name", Type.GetType ("System.String"));
  9. Table.  Columns.Add ("Value", Type.GetType ("System.Int32"));
  10. //Traversal collection
  11. foreach (FieldInfo field in enumfields)
  12. {
  13. if (!field. Isspecialname)
  14. {
  15. DataRow row = table. NewRow ();
  16. Row[0] = field.   Name; //Get field text value
  17. ROW[1] = convert.toint32 (field.        Getrawconstantvalue ()); //Get int value
  18. //row[1] = (int) enum.parse (enumtype, field. Name); You can do that .
  19. Table. Rows.Add (row);
  20. }
  21. }
  22. return table;
  23. }
  24. public static void Setlistcontrol (ListControl list)
  25. {
  26. List. DataSource = Getdatatable ();
  27. List.  DataTextField = "Name";
  28. List.  DataValueField = "Value";
  29. List. DataBind ();
  30. }
  31. }
  32. Public enum Bookingstatus {
  33. Uncommitted = 1,
  34. has been submitted,
  35. has been canceled,
  36. Completed = 6
  37. }
  38. Enummanager<bookingstauts>. Setlistcontrol (Ddlbookingstatus);
  39. Enummanager<ticketstatus>. Setlistcontrol (Rblticketstatus);

C # enumeration bound to ComboBox

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.