C# 将Dictionary,StringDictionary等集合数据绑定到如comboBox等控件数据源中将获取健值
System.Collections.Generic.Dictionary<string, string> dict = new System.Collections.Generic.Dictionary<string, string>();
dict.Add("baidu.com", "百度");
dict.Add("goolge.com", "谷歌");
dict.Add("qq.com", "腾讯");
//绑定数据
comboBox1.DataSource = new BindingSource(dict, null);
comboBox1.ValueMember = "Key";//文本对应的值
comboBox1.DisplayMember = "Value";//显示的文本
//绑定选择事件
comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
//专门存储字符串健值的集合类
//健值都是string类型
System.Collections.Specialized.StringDictionary sd = new System.Collections.Specialized.StringDictionary();
sd.Add("taobao.com", "淘宝");
sd.Add("tamll.com", "天猫");
sd.Add("jd.com", "京东");
comboBox2.DataSource = new BindingSource(sd, null);
comboBox2.ValueMember = "Key";
comboBox2.DisplayMember = "Value";
comboBox2.SelectedIndexChanged += new EventHandler(comboBox2_SelectedIndexChanged);
完整代码:
/******************************************************************
* 创建人:黄愿
* 创建时间:2014-9-9 11:34:39
* 说明:C# 将Dictionary,StringDictionary等集合数据绑定到如comboBox等控件数据源中
* Email:huangyuan413026@163.com
*******************************************************************/
using System;
using System.Windows.Forms;
namespace HTL
{
public partial class BindDictionaryToDatasource : Form
{
public BindDictionaryToDatasource()
{
InitializeComponent();
}
private void BindDictionaryToDatasource_Load(object sender, EventArgs e)
{
System.Collections.Generic.Dictionary<string, string> dict = new System.Collections.Generic.Dictionary<string, string>();
dict.Add("baidu.com", "百度");
dict.Add("goolge.com", "谷歌");
dict.Add("qq.com", "腾讯");
//绑定数据
comboBox1.DataSource = new BindingSource(dict, null);
comboBox1.ValueMember = "Key";//文本对应的值
comboBox1.DisplayMember = "Value";//显示的文本
//绑定选择事件
comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
//专门存储字符串健值的集合类
//健值都是string类型
System.Collections.Specialized.StringDictionary sd = new System.Collections.Specialized.StringDictionary();
sd.Add("taobao.com", "淘宝");
sd.Add("tamll.com", "天猫");
sd.Add("jd.com", "京东");
comboBox2.DataSource = new BindingSource(sd, null);
comboBox2.ValueMember = "Key";
comboBox2.DisplayMember = "Value";
comboBox2.SelectedIndexChanged += new EventHandler(comboBox2_SelectedIndexChanged);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("comboBox1信息如下:\r\n索引:" + comboBox1.SelectedIndex + ",值SelectedValue:" + comboBox1.SelectedValue + ",文本Text:" + comboBox1.Text + ",SelectedItem:" + comboBox1.SelectedItem.ToString());
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("comboBox2信息如下:\r\n索引:" + comboBox2.SelectedIndex + ",值SelectedValue:" + comboBox2.SelectedValue + ",文本Text:" + comboBox2.Text + ",SelectedItem:" + comboBox2.SelectedItem.ToString());
}
}
}
namespace HTL
{
partial class BindDictionaryToDatasource
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.comboBox2 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(, );
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(, );
this.comboBox1.TabIndex = ;
//
// comboBox2
//
this.comboBox2.FormattingEnabled = true;
this.comboBox2.Location = new System.Drawing.Point(, );
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(, );
this.comboBox2.TabIndex = ;
//
// BindDictionaryToDatasource
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.comboBox2);
this.Controls.Add(this.comboBox1);
this.Name = "BindDictionaryToDatasource";
this.Text = "BindDictionaryToDatasource";
this.Load += new System.EventHandler(this.BindDictionaryToDatasource_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.ComboBox comboBox2;
}
}


C# 将Dictionary,StringDictionary等集合数据绑定到如comboBox等控件数据源中将获取健值的更多相关文章
- Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定)
原文:Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定) ------------------------------ ...
- 数据绑定(二)把控件作为Binding源
原文:数据绑定(二)把控件作为Binding源 下面的代码把一个TextBox的Text属性关联在了Slider的Value属性上 <Window x:Class="WpfApplic ...
- 数据绑定技术一:GridView控件
在网站或应用程序中,要显示数据信息,可用到ASP.NET提供的数据源控件和能够显示数据的控件. 一.数据源控件 数据源控件用于连接数据源.从数据源中读取数据以及把数据写入数据源. 1.数据源控件特点 ...
- iOS:集合视图UICollectionView、集合视图控制器UICollectionViewController、集合视图单元格UICollectionViewCell(创建表格的另一种控件)
两种创建表格方式的比较:表格视图.集合视图(二者十分类似) <1>相同点: 表格视图:UITableView(位于storyboard中,通过UIViewController控制器实现 ...
- DataGridView控件用法一:数据绑定
使用DataGridView控件,可以显示和编辑来自多种不同类型的数据源的表格数据. 将数据绑定到DataGridView控件非常简单和直观,在大多数情况下,只需设置DataSource属性即可.在绑 ...
- WP8.1学习系列(第二十三章)——到控件的数据绑定
在本文中 先决条件 将控件绑定到单个项目 将控件绑定到对象的集合 通过使用数据模板显示控件中的项目 添加详细信息视图 转换数据以在控件中显示 相关主题 本主题介绍了如何在使用 C++.C# 或 Vis ...
- 028. asp.net数据绑定控件值DataList控件
DataList控件可以使用模板与定义样式来显示数据并进行数据的选择, 删除及编辑工作. DataList控件的最大特点是一定要通过模板来定义数据的显示格式. 如果要设计出美观的界面, 就需要花费一番 ...
- 027. asp.net中数据绑定控件之 GridView控件
GridView控件支持下面的功能: 绑定至数据源控件, 如SqlDataSource 内置排序功能 内置更新和删除功能 内置分页功能 内置行选择功能 可以编程方式访问GridView对象模型以动态设 ...
- ASP.NET数据绑定控件
数据绑定控件简介 数据绑定分为:数据源 和 数据绑定控件 两部分,数据绑定控件通过数据源来获得数据,通过数据源来隔离数据提供者和数据使用者,数据源有:SqlDataSource,AccessDataS ...
随机推荐
- Java synchronized的原理解析
开始 类有一个特性叫封装,如果一个类,所有的field都是private的,而且没有任何的method,那么这个类就像是四面围墙+天罗地网,没有门.看起来就是一个封闭的箱子,外面的进不来,里面的出不去 ...
- Kylin的垃圾清理
在Kylin运行一段时间之后,有很多数据因为不再使用而变成了垃圾数据,这些数据占据着大量HDFS.HBASE等资源,当积累到一定规模时会对集群性能产生影响.这些垃圾数据主要包括: Purge之后原Cu ...
- javascript类型系统
前面的话 除了对象之外,数组Array类型可能是javascript中最常用的类型了.而且,javascript中的数组与其他多数语言中的数组有着相当大的区别.本文将介绍javascript中的数组A ...
- 比较IBM MQSeries和BEA WebLogic JMS Server(转载)
在面向消息的中间件(MOM)这个领域,IBM MQSeries (又称WebSphere MQ)一直是当仁不让的超级大哥,其它还有一些小兄弟,比如SwiftMQ.SonicMQ之类.但近年来随着J2E ...
- HDU 6118 度度熊的交易计划(费用流)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6118 [题目大意] 给出一张无向边权图,每个点最多可以生产b[i]商品,每件代价为a[i], 每个 ...
- JSONObject 和 JSONArray 的区别和用法
JSONObject 和 JSONArray 的数据表现形式不同: JSONObject的数据是用 { } 来表示的,例如: { "id" : "1", &q ...
- c++string函数详解
string,一个极为好用了函数,学好了这些函数,在模拟以及字符串问题上,回节省很多很多的写代码时间,代码复杂度以及错误率,那么这一类函数都有那些功能呢?我们来逐一介绍(让你大吃一惊,还有这种操作?) ...
- 套题:Codeforces Round #194 (Div. 1) (2/5)
A. Secrets http://www.cnblogs.com/qscqesze/p/4528529.html B. Chips http://www.cnblogs.com/qscqesze/p ...
- Unity UGUI之Button
创建Button后,会出现一个Image组件和一个Button组件,以及Button子节点Text(可以删除不影响功能) 其中Image的Image Type中有四个选项--Simple.Sliced ...
- java 高并发 订单编号递增(解决方案)
业务描述: 首先从数据中查找最近的一条订单数据,然后将之前的订单号码+1作为新的订单号码,插入到数据库当中.(需求不能改变) 当出现并发操作时,A从数据库中获取最近一条订单的订单号为N,这是A还没有完 ...