C# 枚举绑定到ComboBox


方法一:

绑定

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)


方法二:

foreach (var v in typeof(AA).GetFields())
{
     if (v.FieldType.IsEnum == true)
     {
          this.comboBox1.Items.Add(v.Name);
      }
}
this.comboBox1.SelectedIndex = 1;


方法三:

反射,枚举,绑定下拉框

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());        // 获取int数值
              //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 {
    未提交 = 1,
    已提交,
    已取消,
    已完成 = 6
}
EnumManager<BookingStauts>.SetListControl(ddlBookingStatus); 
EnumManager<TicketStatus>.SetListControl(rblTicketStatus);


  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下步骤将枚举绑定combobox上,并实现选择combobox内容后重绘PlotPanel控件: 1. 在WinForms应用程序中,打开Form1.cs文件。 2. 在代码顶部添加以下引用: ```csharp using System.ComponentModel; ``` 3. 在Form1类中定义枚举: ```csharp public enum PlotType { Line, Scatter, Bar } ``` 4. 在Form1类中添加以下方法,该方法将枚举绑定combobox上: ```csharp private void BindPlotTypeComboBox() { BindingList<PlotType> plotTypes = new BindingList<PlotType>(); foreach (PlotType pt in Enum.GetValues(typeof(PlotType))) { plotTypes.Add(pt); } plotTypeComboBox.DataSource = plotTypes; } ``` 5. 在Form1类的构造函数中调用BindPlotTypeComboBox方法: ```csharp public Form1() { InitializeComponent(); BindPlotTypeComboBox(); } ``` 6. 在Form1类中添加以下事件处理程序,该处理程序将在选择combobox内容时重绘PlotPanel控件: ```csharp private void plotTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) { PlotType plotType = (PlotType)plotTypeComboBox.SelectedItem; switch (plotType) { case PlotType.Line: // 绘制线性图 break; case PlotType.Scatter: // 绘制散点图 break; case PlotType.Bar: // 绘制条形图 break; } } ``` 7. 在Form1设计器中,将combobox和PlotPanel控件添加到窗体上,并将combobox的SelectedIndexChanged事件绑定到plotTypeComboBox_SelectedIndexChanged方法。 现在,当你选择combobox的内容时,将会触发plotTypeComboBox_SelectedIndexChanged方法,你可以在该方法中重绘PlotPanel控件,以显示所选枚举类型的图表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值