Combobox must have text and value ~
Steps:
1. Set the data source for combobox ~
ComboBox1.DataSource = list;
List is the data source you want to give to comboBox.
2. Set the text value for comboxBox: comboBox1.DisplayMember = "name"; // all the name field attributes in the set are text
3. Set the value of the header comboxBox: comboBox1.ValueMember = "id"; // the attribute of all id fields in the set is value
My list is a generic set ~
List <User> list = new List <User> ();
// Test Data
List. Add (new User (1, "qq "));
List. Add (new User (2, "bb "));
My User class is the following code (remember to set get method)
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;
}
}
The following describes how to obtain text and value.
Get value MessageBox. Show (comboBox1.SelectedValue. ToString ());
Get text MessageBox. Show (comboBox1.Text );