我将combobox绑定到一张表的一个字段,代码如下 private void Form1_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "data source=10.0.0.122;database=Kindergarten;user id=sa;password=sa";
conn.Open(); SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from BM";
SqlDataAdapter sa = new SqlDataAdapter(cmd.CommandText, conn);
DataSet ds = new DataSet();
sa.Fill(ds);
this.comboBox2.DataSource = ds.Tables[0];
this.comboBox2.DisplayMember = "BM_MC";
this.comboBox2.ValueMember = "BM_DM";
this.comboBox2.SelectedIndex = -1;
conn.Close();
}以上能绑定数据并正常显示,但是我在combobox的下拉箭头中却不能取到值,值为空;
取值代码如下:
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
this.textBox1.Text = this.comboBox2.SelectedText.ToString().Trim();
}
请问这个问题如何解决?
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "data source=10.0.0.122;database=Kindergarten;user id=sa;password=sa";
conn.Open(); SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from BM";
SqlDataAdapter sa = new SqlDataAdapter(cmd.CommandText, conn);
DataSet ds = new DataSet();
sa.Fill(ds);
this.comboBox2.DataSource = ds.Tables[0];
this.comboBox2.DisplayMember = "BM_MC";
this.comboBox2.ValueMember = "BM_DM";
this.comboBox2.SelectedIndex = -1;
conn.Close();
}以上能绑定数据并正常显示,但是我在combobox的下拉箭头中却不能取到值,值为空;
取值代码如下:
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
this.textBox1.Text = this.comboBox2.SelectedText.ToString().Trim();
}
请问这个问题如何解决?
去掉,
试试,
要不然会不会事件没关联上?
http://topic.csdn.net/u/20071109/10/d0bcd1f8-fa56-4752-ada4-d9cb9381a3f0.html
谢谢
this.comboBox2.SelectedIndex = 0;