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; }
