建立一个类 :

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. layui 的单选框

    <div class="layui-form-item"> <label class="layui-form-label">是否通过&l ...

  2. jquery的toggle动画效果显示隐藏

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. SHELL判断服务是不是正在运行

    使用SHELL脚本进行检查服务开启情况 #!/bin/bash #需要首先安装 yum install nmap -y #检查指定端口是否开启 function checkPortStatus() { ...

  4. codeigniter 使用

    CodeIgniter系列 记录count和分页 对于某个表的不带条件的count,可以简单的用 $total = $this->db->count_all($table_name) 来获 ...

  5. 使用PreloadJS加载图片资源

    一. 使用createjs里的LoadQueue函数实现异步加载图片,监听加载进度 1.实例对象LoadQueue加载队列对象 var queue = new createjs.LoadQueue(f ...

  6. Android图片突出

    概述 今天有个群友问 Android 图片凸出 效果怎么弄,早以前有过类似的需求,整个项目的提示框都是一个背景,背景上方有凸出半张图片,所以用layer-list写了一个背景来实现. 思路 随便画了一 ...

  7. Oracle Linux 6.4(BOND)双网卡绑定实战—附加说明

    操作环境Oracle Linux Server release 6.4内核Linux rac1 2.6.39-400.17.1.el6uek.x86_64 [root@RAC-2 ~]# vi /et ...

  8. Android蓝牙开发教程(三)——蓝牙设备相互通讯

    在上一篇中已经介绍如何连接我们搜索到的蓝牙设备,如果你还没阅读过,建议先看看上一篇文章Android蓝牙开发教程(二)——连接蓝牙设备 在上一篇文章中,无论是自动连接还是被动连接,连接成功后,都是将获 ...

  9. GLSL逐顶点光照[转]

    转载:http://blog.csdn.net/hgl868/article/details/7872350 引言 在OpenGL中有三种类型的光:方向光(directional).点光(point) ...

  10. java -jar xxx.jar

    之前用MyEclipse做了一个可执行jar,点击就可运行的. 今天突然不好用了,错误是: could not find the main class C:\123\abc.jar.Program w ...