WinForm 中 comboBox控件之数据绑定
http://www.cnblogs.com/peterzb/archive/2009/05/30/1491923.html
下面介绍三种对comboBox绑定的方式,分别是泛型中IList和Dictionary,还有数据集DataTable
一、IList
现在我们直接创建一个List集合,然后绑定
View Code
IList<string> list = new List<string>();
list.Add("111111");
list.Add("222222");
list.Add("333333");
list.Add("444444");
comboBox1.DataSource = list;
list.Add("111111");
list.Add("222222");
list.Add("333333");
list.Add("444444");
comboBox1.DataSource = list;
执行后,我们会发现绑定成功,但是 我们知道一般对于下拉框的绑定都会有一个值,一个显示的内容,这个时候我们可以创建一个类,把value和text都封装到这个类,作为list的类型
public class Info
{
public string Id { get; set; }
public string Name { get; set; }
}
{
public string Id { get; set; }
public string Name { get; set; }
}
private void bindCbox()
{
IList<Info> infoList = new List<Info>();
Info info1 = new Info() { Id="1",Name="张三"};
Info info2 = new Info() { Id="2",Name="李四"};
Info info3 = new Info() { Id = "3",Name = "王五" };
infoList.Add(info1);
infoList.Add(info2);
infoList.Add(info3);
comboBox1.DataSource = infoList;
comboBox1.ValueMember = "Id";
comboBox1.DisplayMember = "Name";
}
{
IList<Info> infoList = new List<Info>();
Info info1 = new Info() { Id="1",Name="张三"};
Info info2 = new Info() { Id="2",Name="李四"};
Info info3 = new Info() { Id = "3",Name = "王五" };
infoList.Add(info1);
infoList.Add(info2);
infoList.Add(info3);
comboBox1.DataSource = infoList;
comboBox1.ValueMember = "Id";
comboBox1.DisplayMember = "Name";
}
这个时候我们就可以直接获得值和显示的内容了
二、Dictionary
这个有点特殊,不能直接绑定,需要借助类BindingSource才可以完成绑定
View Code
Dictionary<int, string> kvDictonary = new Dictionary<int, string>();
kvDictonary.Add(1, "11111");
kvDictonary.Add(2, "22222");
kvDictonary.Add(3, "333333");
BindingSource bs = new BindingSource();
bs.DataSource = kvDictonary;
comboBox1.DataSource = bs;
comboBox1.ValueMember = "Key";
comboBox1.DisplayMember = "Value";
kvDictonary.Add(1, "11111");
kvDictonary.Add(2, "22222");
kvDictonary.Add(3, "333333");
BindingSource bs = new BindingSource();
bs.DataSource = kvDictonary;
comboBox1.DataSource = bs;
comboBox1.ValueMember = "Key";
comboBox1.DisplayMember = "Value";
三、数据集
这个比较常见,很简单
View Code
//数据集绑定
private void BindCombox()
{
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("id");
DataColumn dc2 = new DataColumn("name");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
DataRow dr1 = dt.NewRow();
dr1["id"] = "1";
dr1["name"] = "aaaaaa";
DataRow dr2 = dt.NewRow();
dr2["id"] = "2";
dr2["name"] = "bbbbbb";
dt.Rows.Add(dr1);
dt.Rows.Add(dr2);
comboBox1.DataSource = dt;
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "name";
}
private void BindCombox()
{
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("id");
DataColumn dc2 = new DataColumn("name");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
DataRow dr1 = dt.NewRow();
dr1["id"] = "1";
dr1["name"] = "aaaaaa";
DataRow dr2 = dt.NewRow();
dr2["id"] = "2";
dr2["name"] = "bbbbbb";
dt.Rows.Add(dr1);
dt.Rows.Add(dr2);
comboBox1.DataSource = dt;
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "name";
}
注意:
当我们触发combox的SelectedIndexChanged的事件后,我们在加载窗体的时候就会执行,这点我刚开始也和魅惑,导致容易出错,这点我们可以采取一些方法避免执行,比如可以定义一个变量fig=false
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if(this.fig) { string selectValue = this.cmbAddMember.SelectedValue.ToString(); rtbaddMember.SelectedText = selectValue; } }
那么肯定想在加载窗体后,执行了,所以在加载窗体后我们还要把fig的值设为true
private void SetAutoMessage_Load(object sender, EventArgs e) { loadCombox(); loadMessageTemplet(); fig= true; }
多思考,多创新,才是正道!