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 > )
{
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 绑定枚举方式的更多相关文章
- c# ComboBox绑定枚举
定义枚举 public enum UserLevel { Commmon = , Administrator, Developer } 方法一 private void Method1() { com ...
- ASP.NET Core 四种方式绑定枚举值
前言 本节我们来讲讲在ASP.NET Core MVC又为我们提供了哪些方便,之前我们探讨过在ASP.NET MVC中下拉框绑定方式,这节我们来再来重点看看枚举绑定的方式,充分实现你所能想到的场景,满 ...
- 整理:WPF中Xaml中绑定枚举的写法
原文:整理:WPF中Xaml中绑定枚举的写法 目的:在Combobox.ListBox中直接绑定枚举对象的方式,比如:直接绑定字体类型.所有颜色等枚举类型非常方便 一.首先用ObjectDataPro ...
- 重新想象 Windows 8 Store Apps (54) - 绑定: 增量方式加载数据
[源码下载] 重新想象 Windows 8 Store Apps (54) - 绑定: 增量方式加载数据 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 绑定 通过实 ...
- ListBox和ComboBox绑定数据简单例子
1. 将集合数据绑定到ListBox和ComboBox控件,界面上显示某个属性的内容 //自定义了Person类(有Name,Age,Heigth等属性) List<Person> per ...
- DataTrigger 绑定枚举
在触发器中绑定枚举类型: <ControlTemplate.Triggers> <DataTrigger Binding="{Binding CheckStateEnum} ...
- c#中DropDownList控件绑定枚举数据
c# asp.net 中DropDownList控件绑定枚举数据 1.枚举(enum)代码: private enum heros { 德玛 = , 皇子 = , 大头 = , 剑圣 = , } 如果 ...
- Binding 中 Elementname,Source,RelativeSource 三种绑定的方式
在WPF应用的开发过程中Binding是一个非常重要的部分. 在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的. 这里将实际中碰到过的问题做下汇总记录和理解. 1. so ...
- ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法
转载:http://blog.sina.com.cn/s/blog_629e606f01014d4b.html ComboBox最经常使用的事件就是SelectedIndexChanged.但在将Co ...
随机推荐
- Python 数据类型-2
序列 包括:字符串 列表 元组 索引操作和切片操作 索引操作:可以从序列中抓取一个特定的项目 切片操作: 获取序列的一个切片,即一部分序列 序列的通用方法: len() 求序列的长度 + 连接2个序列 ...
- 洛谷 P1426 小鱼会有危险吗【模拟/题意理解】
题目描述 有一次,小鱼要从A处沿直线往右边游,小鱼第一秒可以游7米,从第二秒开始每秒游的距离只有前一秒的98%.有个极其邪恶的猎人在距离A处右边s米的地方,安装了一个隐蔽的探测器,探测器左右x米之内是 ...
- Swagger2接口注释参数使用数组
allowMultiple = true, paramType = "query", dataType = "string" 输出的就是这样的:Array[st ...
- 利用反编译学习Android
自从2014年底到2015年中,全民创业的热潮就已经席卷全国了,一大批新的创业公司在北上广萌芽,也造成了大量的开发人员需求.扯远了,今天不谈创业潮,聊聊如何通过反编译学习Android.本文只是个人对 ...
- 【java】TreeMap/HashMap的循环迭代中 keySet和entrySet和forEach方式 + map的几种迭代方式
参考链接:https://www.cnblogs.com/crazyacking/p/5573528.html ================================== java紫色代表迭 ...
- Swift 函数的定义与调用(Defining and Calling Functions)
当你定义一个函数时,你能够定义一个或多个有名字和类型的值.作为函数的输入(称为參数.parameters).也能够定义某种类型的值作为函数运行结束的输出(称为返回类型). 每一个函数有个函数名,用来描 ...
- 【重点突破】—— Vue1.0到Vue2.0的变化
前言: 本文参考作者:_So_ 和 我是某慧 的博文,重点梳理Vue1.0升级到Vue2.0后在开发中要注意的不同,以做学习. 组件模板不再支持片段代码,必须有一个顶级元素包裹,例如: ...
- 原生JavaScript技巧大收集100个
原生JavaScript技巧大收集 1.原生JavaScript实现字符串长度截取function cutstr(str, len) { var temp; var icount = 0; var p ...
- 《学习bash》笔记--调试shell程序
在shell中,最简单的调试助手时输出语句echo,能够通过把很多echo语句放到代码中进行调试,但必须花费足够的时间以定位 要查看的信息.可能必须通过很多的输出才干发现要查找的信息. 1.set选项 ...
- 请求php文件的整个流程
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...