一般在使用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. (14) go 结构体

    1. 声明一个结构体 2.属性 如果是 指针 切片 map 需要用make 3.赋值 (2) (3) (4) 4. 正确,字段相同,数据类型相同,名字相同 5.

  2. javascript 原生得到document.Element的方法

    今天这里写这个博客的主要目的是记录一下javascript原生的选择dom的集中方法. 1.document.getElementById.这个方法接收1个参数,就是DOM元素的id(区分大小写),这 ...

  3. TRUNCATE delete

    The operation cannot be rolled back. DROP and TRUNCATE are DDL commands, whereas DELETE is a DML com ...

  4. git实现github仓库和本地仓库同步

    配置git 安装git以后,打开git bash,首先要对git进行配置,输入 git config --global username "你的名字" git config --g ...

  5. 【BZOJ 4665】 4665: 小w的喜糖 (DP+容斥)

    4665: 小w的喜糖 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 94  Solved: 53 Description 废话不多说,反正小w要发喜 ...

  6. 51nod1437 迈克步 单调栈

    考虑一个点作为最小值的区间$[L[i], R[i]]$ 那么这个区间的所有含$i$的子区间最小值都是$v[i]$ 因此,用单调栈求出$L[i], R[i]$后,对$R[i] - L[i] + 1$这个 ...

  7. Codeforces Beta Round #6 (Div. 2 Only) C. Alice, Bob and Chocolate 水题

    C. Alice, Bob and Chocolate 题目连接: http://codeforces.com/contest/6/problem/C Description Alice and Bo ...

  8. 使用sklearn进行交叉验证

    模型评估方法 假如我们有一个带标签的数据集D,我们如何选择最优的模型? 衡量模型好坏的标准是看这个模型在新的数据集上面表现的如何,也就是看它的泛化误差.因为实际的数据没有标签,所以泛化误差是不可能直接 ...

  9. ROS知识(5)----消息与服务的示例

    ROS中已经定义了较多的标准类型的消息,你可以用在这些标准类型的消息上再自定义自己的消息类型.这个在复杂数据传输很有用,例如节点和服务器进行交互时,就可能用到传输多个参数到服务器,并返回相应的结果.为 ...

  10. Java Web UI框架

    1.Aliceui Aliceui是支付宝的样式解决方案,是一套精选的基于 spm 生态圈的样式模块集合,是 Arale 的子集,也是一套模块化的样式命名和组织规范,是写 CSS 的更好方式. git ...