combobox实现text和value

 combobox要有text和value~

 实现步骤:

1.给combobox设置数据源~

comboBox1.DataSource = list;

list就是你要给comboBox的数据源。

2.给comboxBox设置text值:comboBox1.DisplayMember = "name";  //集合中所有的name字段属性为text

3.个头comboxBox设置value值: comboBox1.ValueMember = "id";  //集合中所有的id字段属性为value
我的list是泛型集合~

List<User> list = new List<User>();

//测试数据

list.Add(new User(1, "qq"));

list.Add(new User(2, "bb"));


我的User类是下面的代码(记得要set get方法)

 public class User  

   {      

   private int id;       

  public int Id      

   {           

  get { return id; }           

  set { id = value; }      

   }       

  private string name;      

   public string Name       

  {          

   get { return name; }       

      set { name = value; }    

     }        

 public User() { }    

     public User(int id, string name)    

     {             this.Id = id;         

    this.Name = name;    

     }   

       }
下面是获取text和value的方法

 获取value MessageBox.Show(comboBox1.SelectedValue.ToString());

 获取text MessageBox.Show(comboBox1.Text);

原文地址:https://www.cnblogs.com/holygis/p/2244159.html