c# combobox 绑定枚举方式

建立一个类 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Reflection;
using System.ComponentModel;

namespace WindowsFormsApplication1
{
    public class EnumDescription
    {
        public static string GetEnumDesc(Enum e)
        {
            FieldInfo EnumInfo = e.GetType().GetField(e.ToString());
            DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])EnumInfo.
                GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (EnumAttributes.Length > 0)
            {
                return EnumAttributes[0].Description;
            }
            return e.ToString();
        }
    }
}

页面代码 :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           // Init();
            Init1();
        }

        public  void Init()
        {
            comboBox1.DataSource = System.Enum.GetNames(typeof(ENUm_Type));
        }

        /// <summary>
        /// 反射邦定枚举
        /// </summary>
        private void Init1()
        {
            Array arrs = System.Enum.GetValues(typeof(ENUm_Type));    // 获取枚举的所有值
            DataTable dt = new DataTable();
            dt.Columns.Add("String", Type.GetType("System.String"));
            dt.Columns.Add("Value", typeof(int));
            foreach (var arr in arrs)
            {
                string strText = EnumDescription.GetEnumDesc((ENUm_Type)arr);
                DataRow aRow = dt.NewRow();
                aRow[0] = strText;
                aRow[1] = (int)arr;
                dt.Rows.Add(aRow);
            }

            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = "String";
            comboBox1.ValueMember = "Value";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 第一种实现方式
            //ENUm_Type eT = ENUm_Type.tet_1;
            //comboBox1.SelectedIndex = comboBox1.FindString(eT.ToString());

            //string str  =   comboBox1.SelectedItem.ToString();

            // 第二种实现方式
            int a = comboBox1.SelectedIndex;
            System.Diagnostics.Trace.WriteLine(comboBox1.SelectedItem);
            DataRowView dr = (DataRowView)(comboBox1.SelectedItem);
            ENUm_Type aE = (ENUm_Type)(dr.Row[1]);
        }

        public enum ENUm_Type
        {
            [Description("tet_1")]
             tet_1 = 1,
            [Description("tet_2")]
            tet_2 = 2,
            [Description("tet_3")]
            tet_3 = 3,
        }
    }

}
时间: 2024-08-05 15:55:35

c# combobox 绑定枚举方式的相关文章

ASP.NET Core 四种方式绑定枚举值

原文:ASP.NET Core 四种方式绑定枚举值 前言 本节我们来讲讲在ASP.NET Core MVC又为我们提供了哪些方便,之前我们探讨过在ASP.NET MVC中下拉框绑定方式,这节我们来再来重点看看枚举绑定的方式,充分实现你所能想到的场景,满满的干货,你值得拥有. 探讨枚举绑定方式# 我们首先给出要绑定的枚举类. public enum Language { JavaScript, Java, C, Python, SQL, Oracle } 枚举绑定方式一(@Html.DropDow

WinForm 中ComboBox 绑定总结

http://www.cnblogs.com/blsong/archive/2010/04/13/1710955.html 1. DataTable用DataTable直接绑定,只需 要设置DataSource.DisplayMember.ValueMember三个属性即可. this.cmbConsumeSuperMarket.DataSource = dtSuperMarket;this.cmbConsumeSuperMarket.DisplayMember = "Name"; t

winform中的ListBox和ComboBox绑定数据用法实例

本文实例讲述了winform中的ListBox和ComboBox绑定数据用法.分享给大家供大家参考.具体实现方法如下: 本例实现将集合数据绑定到ListBox和ComboBox控件,界面上显示某个属性的内容,代码如下: 代码如下: //- //自定义了Person类(有Name,Age,Heigth等属性) List<Person> persons=new List<Person>(); persons.Add(new Person("WuMiao",18,17

DataTrigger 绑定枚举

在触发器中绑定枚举类型: <ControlTemplate.Triggers> <DataTrigger Binding="{Binding CheckStateEnum}" Value="WaitCheck"> <Setter TargetName="Border" Property="Background" Value="{DynamicResource ListViewItemNo

c#中DropDownList控件绑定枚举数据

c# asp.net 中DropDownList控件绑定枚举数据 1.枚举(enum)代码: private enum heros { 德玛 = 0, 皇子 = 10, 大头 = 20, 剑圣 = 30, } 如果不为枚举数列表中的元素指定值,则它们的值将自动递增,从1开始. 2.cs代码: private void LoadData() { //检索枚举heros返回包含每个成员的值的数组 Array herosArray = Enum.GetValues(typeof(heros)); fo

Binding 中 Elementname,Source,RelativeSource 三种绑定的方式

在WPF应用的开发过程中Binding是一个非常重要的部分. 在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的. 这里将实际中碰到过的问题做下汇总记录和理解. 1. source = {binding} 和source = {binding RelativeSource={RelativeSource self},Path=DataContext}效果相同 理解:{binding} 不设定明确的绑定的source,这样binding就去从本控件类为开始根据可视树的层

WPF{ComboBox绑定类对象, 下拉列显示的值,与取到的值}

DisplayMemberPath 是用来显示下拉列表的值 SelectedValuePath是用来取得选中项的值. ComboBox绑定类对象, 下拉列显示的值,与取到的值 string. Join的作用 输出结果是

多个ComboBox绑定同一个数据源出现的问题解决办法

出现问题: 当多个ComboBox绑定同一个数据源后,只要更改其中一个的选择项时,其它的ComboBox也跟着改变了 解决办法: DataTable dt = new DataTable(); dt = Common.XmlToDatatable(mPath, "常用四至"); DataRow dr = dt.NewRow(); dr["四至名称"] = "";//添加空行 dt.Rows.InsertAt(dr, 0); cmbDZ.DataS

绑定枚举到dropdownlist

pageTools.BindEnumToDropdownList(typeof(enumDealerArea), ddlBmwArea, new ListItem("--请选择--", "0"), ""); /// <summary>    /// 绑定枚举到dropdownlist    /// </summary>    /// <param name="enumType">枚举类型&l