建立一个类 :

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 > )
{
return EnumAttributes[].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[] = strText;
aRow[] = (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[]);
} public enum ENUm_Type
{
[Description("tet_1")]
tet_1 = ,
[Description("tet_2")]
tet_2 = ,
[Description("tet_3")]
tet_3 = ,
}
} }

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

  1. c# ComboBox绑定枚举

    定义枚举 public enum UserLevel { Commmon = , Administrator, Developer } 方法一 private void Method1() { com ...

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

    前言 本节我们来讲讲在ASP.NET Core MVC又为我们提供了哪些方便,之前我们探讨过在ASP.NET MVC中下拉框绑定方式,这节我们来再来重点看看枚举绑定的方式,充分实现你所能想到的场景,满 ...

  3. 整理:WPF中Xaml中绑定枚举的写法

    原文:整理:WPF中Xaml中绑定枚举的写法 目的:在Combobox.ListBox中直接绑定枚举对象的方式,比如:直接绑定字体类型.所有颜色等枚举类型非常方便 一.首先用ObjectDataPro ...

  4. 重新想象 Windows 8 Store Apps (54) - 绑定: 增量方式加载数据

    [源码下载] 重新想象 Windows 8 Store Apps (54) - 绑定: 增量方式加载数据 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 绑定 通过实 ...

  5. ListBox和ComboBox绑定数据简单例子

    1. 将集合数据绑定到ListBox和ComboBox控件,界面上显示某个属性的内容 //自定义了Person类(有Name,Age,Heigth等属性) List<Person> per ...

  6. DataTrigger 绑定枚举

    在触发器中绑定枚举类型: <ControlTemplate.Triggers> <DataTrigger Binding="{Binding CheckStateEnum} ...

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

    c# asp.net 中DropDownList控件绑定枚举数据 1.枚举(enum)代码: private enum heros { 德玛 = , 皇子 = , 大头 = , 剑圣 = , } 如果 ...

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

    在WPF应用的开发过程中Binding是一个非常重要的部分. 在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的. 这里将实际中碰到过的问题做下汇总记录和理解. 1. so ...

  9. ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法

    转载:http://blog.sina.com.cn/s/blog_629e606f01014d4b.html ComboBox最经常使用的事件就是SelectedIndexChanged.但在将Co ...

随机推荐

  1. Virtualbox 设置虚拟机上网并和主机互通(如ping等)

    我的主机是Ubuntu12.04, 安装virtualbox虚拟了一个xp系统.把xp作为一个开发用的机器,需要上网,并且和主机以及虚拟机之间互相访问. 1. 在virtual设置界面,将xp系统的网 ...

  2. 全局变量 全局函数vue 方法

    定义全局变量 原理: 设置一个专用的的全局变量模块文件,模块里面定义一些变量初始状态,用export default 暴露出去,在main.js里面使用Vue.prototype挂载到vue实例上面或 ...

  3. SQLite中特殊的INSERT语句

    SQLite中特殊的INSERT语句   在SQLite中,INSERT是基本语句,用来向表中插入数据.但是当表中存在字段存在唯一.非空.检查.主键等约束时,插入的数据很容易和约束冲突,造成插入操作失 ...

  4. luogu P2744 [USACO5.3]量取牛奶Milk Measuring

    题目描述 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位——译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出.消费者要多少,他就给多少,从不有 ...

  5. 打包python脚本为exe

    更新pip 安装

  6. Mac outlook设置自动回复

    outlook是公司必不可少的软件, 在mac下开发,当然用的是mac版的outlook,今天介绍一下如何设置mac下outlook的自动回复. 有两种方式的帐号,一种是Exchange accoun ...

  7. DB2和MySQL常用SQL整理

    1.Truncate删除表中所有数据 truncate table USER immediate; 说明:Truncate是一个能够快速清空资料表内所有资料的SQL语法.并且能针对具有自动递增值的字段 ...

  8. k8s学习(一)——kubectl与api-server之间的交互核心过程

    k8s的架构是用户使用kubectl工具对虚拟机资源进行各种各样的控制和定制. 而kubectl本身并不包含对其核心资源的访问与控制.而是通过http通信与api-server进行交互实现资源的管理. ...

  9. 【重点突破】—— Vue1.0到Vue2.0的变化

    前言: 本文参考作者:_So_ 和 我是某慧 的博文,重点梳理Vue1.0升级到Vue2.0后在开发中要注意的不同,以做学习.        组件模板不再支持片段代码,必须有一个顶级元素包裹,例如: ...

  10. React学习之redux

    在阅读本文之前,希望大家对以下知识点能提前有所了解并且上好厕所(文章有点长): 状态提升的概念 react高阶组件(函数) es6基础 pure 组件(纯函数) Dumb 组件 React.js的co ...