一般在使用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. NOIP2013 D1 T3 货车运输

    好吧,遇上这种题,作为蒟蒻的我第一个想到的就是怎么打暴力,然而暴力都打不好QAQ!!!于是只能等教练讲解以后,然后在大犇的指导下终于做出来了. 对了,,好像还,没上题....: 题目描述 A 国有 n ...

  2. 最大流 [USACO4.2]草地排水Drainage Ditches

    Background 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系统来使贝茜的草地免 ...

  3. Xcode代码提示里的字母含义

    P -- 协议 M -- 成员方法 C -- 类 K -- 枚举 .常量 V -- 成员变量 T -- typedef类型 G -- 全局变量 f -- 函数 # -- #define指令

  4. Scrapy实战篇(四)之周杰伦到底唱了啥

    从小到大,一直很喜欢听周杰伦唱的歌,可是相信很多人和我一样,并不能完全听明白歌词究竟是什么,今天我们就来研究一下周董最喜欢在歌词中用的词,这一小节的构思是这样的,我们爬取周杰伦的歌词信息,并且将其进行 ...

  5. android studio 继续汉化 编译项目 菜单

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha Edit Flavors...

  6. Linux下C语言多文件的编译以及makefile的应用

    1.关于编译和链接 一般来说,无论是C.C++,首先要把源文件编译成中间代码文件,在Windows下也就是.obj文件,UNIX下是.o文件,即Object File,这个动作叫做编译(compile ...

  7. [CODECHEF]EASYEX

    题意:有一个$k$面的骰子,上面的数字为$1\cdots k$,现在要丢$n$次骰子,设$n$次中有$a_i$次扔到数字$i$,给定$l,f$,求$\prod\limits_{i=1}^la_i^f$ ...

  8. 两个函数彻底理解Lua中的闭包

    本文通过两个函数彻底搞懂Lua中的闭包,相信看完这两个函数,应该能理解什么是Lua闭包.废话不多说,上 code: --[[************************************** ...

  9. Codeforces Beta Round #7 C. Line Exgcd

    C. Line 题目连接: http://www.codeforces.com/contest/7/problem/C Description A line on the plane is descr ...

  10. mysql 绿色版安装

    1. 下载MySql5.6.10GA解压缩版,这就不多说也不上图了,下不到或者下好之后不知道如何解压的接下去的文章也没什么好多看的. 2. 解压好之后进入根目录是这样个情况(本人使用的是MySql5. ...