C# 将Dictionary,StringDictionary等集合数据绑定到如comboBox等控件数据源中将获取健值...

一般在使用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());
        }
    }
}
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;
    }
}
View Code

 

参考:



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值