一般在使用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信息如下: 索引:" + comboBox1.SelectedIndex + ",值SelectedValue:" + comboBox1.SelectedValue + ",文本Text:" + comboBox1.Text + ",SelectedItem:" + comboBox1.SelectedItem.ToString()); } private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { MessageBox.Show("comboBox2信息如下: 索引:" + comboBox2.SelectedIndex + ",值SelectedValue:" + comboBox2.SelectedValue + ",文本Text:" + comboBox2.Text + ",SelectedItem:" + comboBox2.SelectedItem.ToString()); } } }
设计代码:
View Code
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(107, 69); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(121, 20); this.comboBox1.TabIndex = 0; // // comboBox2 // this.comboBox2.FormattingEnabled = true; this.comboBox2.Location = new System.Drawing.Point(107, 127); this.comboBox2.Name = "comboBox2"; this.comboBox2.Size = new System.Drawing.Size(121, 20); this.comboBox2.TabIndex = 1; // // BindDictionaryToDatasource // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 273); 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; } }
参考: