一般在使用C#提供的如combobx控件绑定数据源时都是直接绑定数据库里的数据的(DataTable,DataSet等)
最近在一个项目里需要使用combobox绑定类似“状态的”数据源,该字段里的数据最不可变,所以不需要从数据库里获取数据
由于字段只需要健值没有其他信息所以使用Dictionary是最合适不过了,但如何将集合里的数据绑定并再获取集合里的健值呢?
在google搜索一番并找到解决方案:
  1. System.Collections.Generic.Dictionary<string, string> dict = new System.Collections.Generic.Dictionary<string, string>();
  2. dict.Add("baidu.com", "百度");
  3. dict.Add("goolge.com", "谷歌");
  4. dict.Add("qq.com", "腾讯");
  5. //绑定数据
  6. comboBox1.DataSource = new BindingSource(dict, null);
  7. comboBox1.ValueMember = "Key";//文本对应的值
  8. comboBox1.DisplayMember = "Value";//显示的文本
  9. //绑定选择事件
  10. comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
  11. //专门存储字符串健值的集合类
  12. //健值都是string类型
  13. System.Collections.Specialized.StringDictionary sd = new System.Collections.Specialized.StringDictionary();
  14. sd.Add("taobao.com", "淘宝");
  15. sd.Add("tamll.com", "天猫");
  16. sd.Add("jd.com", "京东");
  17. comboBox2.DataSource = new BindingSource(sd, null);
  18. comboBox2.ValueMember = "Key";
  19. comboBox2.DisplayMember = "Value";
  20. comboBox2.SelectedIndexChanged += new EventHandler(comboBox2_SelectedIndexChanged);

完整代码:

  1. /******************************************************************
  2. * 创建人:黄愿
  3. * 创建时间:2014-9-9 11:34:39
  4. * 说明:C# 将Dictionary,StringDictionary等集合数据绑定到如comboBox等控件数据源中
  5. * Email:huangyuan413026@163.com
  6. *******************************************************************/
  7. using System;
  8. using System.Windows.Forms;
  9. namespace HTL
  10. {
  11. public partial class BindDictionaryToDatasource : Form
  12. {
  13. public BindDictionaryToDatasource()
  14. {
  15. InitializeComponent();
  16. }
  17. private void BindDictionaryToDatasource_Load(object sender, EventArgs e)
  18. {
  19. System.Collections.Generic.Dictionary<string, string> dict = new System.Collections.Generic.Dictionary<string, string>();
  20. dict.Add("baidu.com", "百度");
  21. dict.Add("goolge.com", "谷歌");
  22. dict.Add("qq.com", "腾讯");
  23. //绑定数据
  24. comboBox1.DataSource = new BindingSource(dict, null);
  25. comboBox1.ValueMember = "Key";//文本对应的值
  26. comboBox1.DisplayMember = "Value";//显示的文本
  27. //绑定选择事件
  28. comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
  29. //专门存储字符串健值的集合类
  30. //健值都是string类型
  31. System.Collections.Specialized.StringDictionary sd = new System.Collections.Specialized.StringDictionary();
  32. sd.Add("taobao.com", "淘宝");
  33. sd.Add("tamll.com", "天猫");
  34. sd.Add("jd.com", "京东");
  35. comboBox2.DataSource = new BindingSource(sd, null);
  36. comboBox2.ValueMember = "Key";
  37. comboBox2.DisplayMember = "Value";
  38. comboBox2.SelectedIndexChanged += new EventHandler(comboBox2_SelectedIndexChanged);
  39. }
  40. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  41. {
  42. MessageBox.Show("comboBox1信息如下:\r\n索引:" + comboBox1.SelectedIndex + ",值SelectedValue:" + comboBox1.SelectedValue + ",文本Text:" + comboBox1.Text + ",SelectedItem:" + comboBox1.SelectedItem.ToString());
  43. }
  44. private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
  45. {
  46. MessageBox.Show("comboBox2信息如下:\r\n索引:" + comboBox2.SelectedIndex + ",值SelectedValue:" + comboBox2.SelectedValue + ",文本Text:" + comboBox2.Text + ",SelectedItem:" + comboBox2.SelectedItem.ToString());
  47. }
  48. }
  49. }
设计代码:

  1. namespace HTL
  2. {
  3. partial class BindDictionaryToDatasource
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9. /// <summary>
  10. /// Clean up any resources being used.
  11. /// </summary>
  12. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  13. protected override void Dispose(bool disposing)
  14. {
  15. if (disposing && (components != null))
  16. {
  17. components.Dispose();
  18. }
  19. base.Dispose(disposing);
  20. }
  21. #region Windows Form Designer generated code
  22. /// <summary>
  23. /// Required method for Designer support - do not modify
  24. /// the contents of this method with the code editor.
  25. /// </summary>
  26. private void InitializeComponent()
  27. {
  28. this.comboBox1 = new System.Windows.Forms.ComboBox();
  29. this.comboBox2 = new System.Windows.Forms.ComboBox();
  30. this.SuspendLayout();
  31. //
  32. // comboBox1
  33. //
  34. this.comboBox1.FormattingEnabled = true;
  35. this.comboBox1.Location = new System.Drawing.Point(, );
  36. this.comboBox1.Name = "comboBox1";
  37. this.comboBox1.Size = new System.Drawing.Size(, );
  38. this.comboBox1.TabIndex = ;
  39. //
  40. // comboBox2
  41. //
  42. this.comboBox2.FormattingEnabled = true;
  43. this.comboBox2.Location = new System.Drawing.Point(, );
  44. this.comboBox2.Name = "comboBox2";
  45. this.comboBox2.Size = new System.Drawing.Size(, );
  46. this.comboBox2.TabIndex = ;
  47. //
  48. // BindDictionaryToDatasource
  49. //
  50. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  51. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  52. this.ClientSize = new System.Drawing.Size(, );
  53. this.Controls.Add(this.comboBox2);
  54. this.Controls.Add(this.comboBox1);
  55. this.Name = "BindDictionaryToDatasource";
  56. this.Text = "BindDictionaryToDatasource";
  57. this.Load += new System.EventHandler(this.BindDictionaryToDatasource_Load);
  58. this.ResumeLayout(false);
  59. }
  60. #endregion
  61. private System.Windows.Forms.ComboBox comboBox1;
  62. private System.Windows.Forms.ComboBox comboBox2;
  63. }
  64. }
参考:

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. 【我要学python】函数的系统学习

    我的短期目标:python+CTF reverse 一起加油! #1,函数介绍 1,功能性 函数目的 2,隐藏性 (封装) (避免写重复代码) 例: #round为保留小数的函数 a = 3.1415 ...

  2. springMVC整合freemarker遇到的问题 maven

    java.lang.IllegalAccessError: tried to access method freemarker.ext.servlet.AllHttpScopesHashModel.& ...

  3. Xamarin.Forms XAML的辅助功能Code Snippet

    Xamarin.Forms XAML的辅助功能Code Snippet   在Visual Studio中,使用Code Snippet(代码片段)功能可以减少基础代码的编写量,如常见的标签.循环语句 ...

  4. Kail Linux渗透测试教程之免杀Payload生成工具Veil

    Kail Linux渗透测试教程之免杀Payload生成工具Veil 免杀Payload生成工具——Veil Kail Linux渗透测试教程之免杀Payload生成工具Veil,Veil是一款利用M ...

  5. [转]iOS开发new与alloc/init的区别

    1.在实际开发中很少会用到new,一般创建对象咱们看到的全是[[className alloc] init] 但是并不意味着你不会接触到new,在一些代码中还是会看到[className new], ...

  6. Python并发编程-进程

    由于GIL(全局解释锁)的问题,多线程并不能充分利用多核处理器,如果是一个CPU计算型的任务,应该使用多进程模块 multiprocessing .它的工作方式与线程库完全不同,但是两种库的语法和接口 ...

  7. Vue 2.0学习(一)简介

    简介 Vue是一套用于构建用户界面的渐进式框架.简单小巧( 压缩后仅17KB),Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易于上手,还便于与第三方库或既 ...

  8. Redis学习篇(五)之Set类型及其操作

    SADD 作用:向集合中添加元素 语法:SADD key menber [, ...] 添加重复的元素会返回0,失败 SMEMBERS 作用:返回指定集合中的元素 语法: SMEMBERS key S ...

  9. 【hihoCoder 1513】小Hi的烦恼

    http://hihocoder.com/problemset/problem/1513 h[j][i]记录第j个学科排名小于等于i的状态,bitset压位就可以了. #include<bits ...

  10. 我的OI生涯 第四章

    第四章 晚上来机房的人越来越多了,我也注意到一个常年独自坐在一个角落的男人————郝哥. 郝哥为人很安静,只是那时我还不知道他好不好,就没有与他交流过什么,这个优秀的男人以后我们还会提到,这里先不讲. ...