|
Specify DisplayMember and ValueMember when binding.
for example:
A table studentDetails, two fields (sno, sname), bound to Commbobox:
//Binding when FormLoad, display name
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421");
SqlDataAdapter sda = new SqlDataAdapter("select * from studentDetails", con);
DataSet ds = new DataSet();
sda.Fill(ds, "student");
this.comboBox1.DataSource = ds.Tables["student"];
this.comboBox1.DisplayMember = "sname";
this.comboBox1.ValueMember = "sno";
}
//Get the value of the currently selected item, that is, the student ID
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
this.textBox1.Text = this.comboBox1.SelectedValue.ToString();
} |
|