|
发表于 2021-3-2 14:00:01
|
显示全部楼层
绑定时指定DisplayMember和ValueMember..
for example:
一个表studentDetails,两个字段(sno,sname),绑定到Commbobox:
//FormLoad时绑定,显示姓名
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";
}
//得到当前选中项的value,即学号
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
this.textBox1.Text = this.comboBox1.SelectedValue.ToString();
} |
|