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 ...
随机推荐
- 【我要学python】函数的系统学习
我的短期目标:python+CTF reverse 一起加油! #1,函数介绍 1,功能性 函数目的 2,隐藏性 (封装) (避免写重复代码) 例: #round为保留小数的函数 a = 3.1415 ...
- springMVC整合freemarker遇到的问题 maven
java.lang.IllegalAccessError: tried to access method freemarker.ext.servlet.AllHttpScopesHashModel.& ...
- Xamarin.Forms XAML的辅助功能Code Snippet
Xamarin.Forms XAML的辅助功能Code Snippet 在Visual Studio中,使用Code Snippet(代码片段)功能可以减少基础代码的编写量,如常见的标签.循环语句 ...
- Kail Linux渗透测试教程之免杀Payload生成工具Veil
Kail Linux渗透测试教程之免杀Payload生成工具Veil 免杀Payload生成工具——Veil Kail Linux渗透测试教程之免杀Payload生成工具Veil,Veil是一款利用M ...
- [转]iOS开发new与alloc/init的区别
1.在实际开发中很少会用到new,一般创建对象咱们看到的全是[[className alloc] init] 但是并不意味着你不会接触到new,在一些代码中还是会看到[className new], ...
- Python并发编程-进程
由于GIL(全局解释锁)的问题,多线程并不能充分利用多核处理器,如果是一个CPU计算型的任务,应该使用多进程模块 multiprocessing .它的工作方式与线程库完全不同,但是两种库的语法和接口 ...
- Vue 2.0学习(一)简介
简介 Vue是一套用于构建用户界面的渐进式框架.简单小巧( 压缩后仅17KB),Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易于上手,还便于与第三方库或既 ...
- Redis学习篇(五)之Set类型及其操作
SADD 作用:向集合中添加元素 语法:SADD key menber [, ...] 添加重复的元素会返回0,失败 SMEMBERS 作用:返回指定集合中的元素 语法: SMEMBERS key S ...
- 【hihoCoder 1513】小Hi的烦恼
http://hihocoder.com/problemset/problem/1513 h[j][i]记录第j个学科排名小于等于i的状态,bitset压位就可以了. #include<bits ...
- 我的OI生涯 第四章
第四章 晚上来机房的人越来越多了,我也注意到一个常年独自坐在一个角落的男人————郝哥. 郝哥为人很安静,只是那时我还不知道他好不好,就没有与他交流过什么,这个优秀的男人以后我们还会提到,这里先不讲. ...