C#中为ComboBox设定value值

 

推荐一个我自己做的一个电影站,绝对没弹窗:-)  www.fastdy.com 速看电影吧,每天都更新,

最新的电影电视剧都在速看电影吧!~

 

 

(1)自己手动添加值

先定义一个类似如下的结构体:

public struct MyItem
{
 public string Text;
 public int Value;//你需要的值类型
 public MyItem(string text, int value)
 {
  this.Text = text;
  this.Value = value;
 }
 public override string ToString()
 {
  return Text;
 }
}

然后在需要添加和使用的地方添加类似如下的代码:

//添加
this.Combobox1.Item.Add(new MyItem("Text1", 1));
this.Combobox1.Item.Add(new MyItem("Text2", 2));

//取得值

MessageBox.Show(( (MyItem)(ComboBox1.SelectedItem)).Value.ToString() );

(2)数据绑定

comboBox1.DataSource = datatable;
comboBox1.DisplayMember = datatable.Columns[0].ToString(); //显示的值
comboBox1.ValueMember = datatable.Columns[1].ToString();  //实际值

posted @ 2011-07-28 13:54  K.chaos  阅读(1640)  评论(0)    收藏  举报