一般在使用C#提供的如combobx控件绑定数据源时都是直接绑定数据库里的数据的(DataTable,DataSet等)
最近在一个项目里需要使用combobox绑定类似“状态的”数据源,该字段里的数据最不可变,所以不需要从数据库里获取数据
由于字段只需要健值没有其他信息所以使用Dictionary是最合适不过了,但如何将集合里的数据绑定并再获取集合里的健值呢?
在google搜索一番并找到解决方案:
 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等控件数据源中将获取健值的更多相关文章

  1. Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定)

    原文:Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定) ------------------------------ ...

  2. 数据绑定(二)把控件作为Binding源

    原文:数据绑定(二)把控件作为Binding源 下面的代码把一个TextBox的Text属性关联在了Slider的Value属性上 <Window x:Class="WpfApplic ...

  3. 数据绑定技术一:GridView控件

    在网站或应用程序中,要显示数据信息,可用到ASP.NET提供的数据源控件和能够显示数据的控件. 一.数据源控件 数据源控件用于连接数据源.从数据源中读取数据以及把数据写入数据源. 1.数据源控件特点 ...

  4. iOS:集合视图UICollectionView、集合视图控制器UICollectionViewController、集合视图单元格UICollectionViewCell(创建表格的另一种控件)

    两种创建表格方式的比较:表格视图.集合视图(二者十分类似) <1>相同点:   表格视图:UITableView(位于storyboard中,通过UIViewController控制器实现 ...

  5. DataGridView控件用法一:数据绑定

    使用DataGridView控件,可以显示和编辑来自多种不同类型的数据源的表格数据. 将数据绑定到DataGridView控件非常简单和直观,在大多数情况下,只需设置DataSource属性即可.在绑 ...

  6. WP8.1学习系列(第二十三章)——到控件的数据绑定

    在本文中 先决条件 将控件绑定到单个项目 将控件绑定到对象的集合 通过使用数据模板显示控件中的项目 添加详细信息视图 转换数据以在控件中显示 相关主题 本主题介绍了如何在使用 C++.C# 或 Vis ...

  7. 028. asp.net数据绑定控件值DataList控件

    DataList控件可以使用模板与定义样式来显示数据并进行数据的选择, 删除及编辑工作. DataList控件的最大特点是一定要通过模板来定义数据的显示格式. 如果要设计出美观的界面, 就需要花费一番 ...

  8. 027. asp.net中数据绑定控件之 GridView控件

    GridView控件支持下面的功能: 绑定至数据源控件, 如SqlDataSource 内置排序功能 内置更新和删除功能 内置分页功能 内置行选择功能 可以编程方式访问GridView对象模型以动态设 ...

  9. ASP.NET数据绑定控件

    数据绑定控件简介 数据绑定分为:数据源 和 数据绑定控件 两部分,数据绑定控件通过数据源来获得数据,通过数据源来隔离数据提供者和数据使用者,数据源有:SqlDataSource,AccessDataS ...

随机推荐

  1. 20169211《Linux内核原理与分析》第四周作业

    20169211<Linux内核原理与分析>第四周作业内容列表 1.教材第3.5章节知识学习总结: 2.实验楼配套实验二实验报告: 1.<linux内核设计与实现>教材第3.5 ...

  2. Python数据类型-集合(set)

    1.创建集合 集合的创建不同于前两种数据结构. 集合通过set(iterable)方法创建,参数iterable为可迭代对象. 示例代码: s1 = set('好好学习天天想上') # 将字符串分解为 ...

  3. 第9天-BOM和DOM

    什么是DOM 文档对象模型(Document Object Model),DOM作用:可以去修改网页内容.样式.结构. 每个浏览器都会把html文档解析成dom树,就可以用相关方法和属性操作网页元素 ...

  4. Java 8中你可能没听过的10个新特性

    lambda表达式,lambda表达式,还是lambda表达式.一提到Java 8就只能听到这个,但这不过是其中的一个新功能而已,Java 8还有许多新的特性——有一些功能强大的新类或者新的用法,还有 ...

  5. 【Python】闭包Closure

    原来这就是闭包啊... 还是上次面试,被问只不知掉js里面的闭包 闭包,没听过啊...什么是闭包 回来查了下,原来这货叫闭包啊...... —————————————————————————————— ...

  6. TCP三次握手,什么情况下client会回复reset

    1. 现象 最近线上发现如下异常包, tcp三次握手期间,server端发送syn_ack,client回复了reset包: 问题:为什么client会回复reset? 2. 分析 参考linux2. ...

  7. 深入理解javascript作用域系列第四篇

    前面的话 尽管函数作用域是最常见的作用域单元,也是现行大多数javascript最普遍的设计方法,但其他类型的作用域单元也是存在的,并且通过使用其他类型的作用域单元甚至可以实现维护起来更加优秀.简洁的 ...

  8. poj2117 Electricity

      试题描述 求一个图删除一个点之后,联通块最多有多少. 输入 多组数据.第一行两个整数 P,C 表示点数和边数.接下来 C 行每行两个整数 p1,p2,表示 p1 与 p2 有边连接,保证无重边.读 ...

  9. bzoj 2565: 最长双回文串 manacher算法

    2565: 最长双回文串 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem. ...

  10. acdream 1735 输油管道 贪心

    输油管道 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acdream.info/problem?pid=1735 Description ...