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 ...
随机推荐
- LeetCode OJ-- Container With Most Water
https://oj.leetcode.com/problems/container-with-most-water/ 不同高度的柱子排一列,两个柱子可以组成一个容器,求最大容积. 最直观的方法就是暴 ...
- ()centos6.8安装配置ftp服务器
ftp传输原理 客户端通过某软件用某个端口(a端口)向服务端发起tcp连接请求,同时告诉服务端客户端另一个空闲端口号(b端口),服务端用21端口与客户端建立一条控制连接通道. 接着在默认情况下,服务端 ...
- NOIP 2014飞扬的小鸟(DP优化)
题目链接 飞扬的小鸟 考场的70分暴力(实际只有50分因为数组开小了……) 考场代码(数组大小已修改) #include <cstdio> #include <cstring> ...
- 生成唯一标识符 ,通用唯一标识符 UUID
import java.util.UUID; /** * UUID生成工具 * */ public final class UuidGenUtils { /** * 生成一个UUID串(32个字符,其 ...
- shell中eval的使用问题
转载:http://www.blogjava.net/jasmine214--love/archive/2010/11/26/339106.html 本文将会讲解一些linux中命令的使用与技巧希望对 ...
- iOS升级经验分享
作者认为,及时关注.快速反应.覆盖测试是面对iOS系统升级时最重要的三大原则,文中还详细分析了iCloud Storage和Automatic Reference Counting这两大iOS 5新特 ...
- Android Studio +MAT 分析内存泄漏实战
对于内存泄漏,在Android中如果不注意的话,还是很容易出现的,尤其是在Activity中,比较容易出现,下面我就说下自己是如何查找内存泄露的. 首先什么是内存泄漏? 内存泄漏就是一些已经不使用的对 ...
- C#面试基础题1
1.简述 private. protected. public. internal 修饰符的访问权限.(C++中没有internal) private : 私有成员, 在类的内部才可以访问 ,也就是类 ...
- Web编程前端之7:web.config详解 【转】
http://www.cnblogs.com/alvinyue/archive/2013/05/06/3063008.html 声明:这篇文章是摘抄周公(周金桥)的<asp.net夜话> ...
- ElasticSearch查询max_result_window问题处理
需要出一份印地语文章的表,导出规则为: 1.所有印地语(包含各种颜色,各种状态)的文章 2.阅读数大于300 3.按照阅读推荐比进行排序,取前3000篇文章 说明: 1.文章信息,和阅读推荐数量在两个 ...