日期:2014-05-20  浏览次数:20839 次

提一个关于ComboBox的问题?
在网页上有个下拉表单:
<select>
<option   value= "值 "> text </option>
</select>
我们能看到的是 <option> 项的text,选择这项就可以得到value= "值 "。

相对应的C#   Form里面的ComboBox控件是
我们看到的是Items.Text属性,但是它每项所对应的value属性怎么赋值了?

------解决方案--------------------
this.cmbSno.DataSource=ds.Tables[ "student "];
this.cmbSno.DisplayMember= "Sno ";
this.cmbSno.ValueMember= "Sno ";
------解决方案--------------------
绑定时指定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();
}