(假设我的数据库表名是cp,其中有主键id,cpbh,cpmc)   
  现在要在listbox控件中加入cpmc,当点中listbox中某一个字段值时,可以在数据库中查询id号等于当前选中的字段值,在textbox1中显示它id的内容,在textbox2中显示cpbh的内容  

解决方案 »

  1.   

    private void listbox_selectedIndexChanged(object sender, EventArgs e)
    {
    SqlConnection con = new SqlConnection(连接串);
    SqlCommand cmd = new SqlCommand("Select id,cpbh from cp where cpmc=@cpmc",con);  
    cmd.Parameters.AddWithValue("@cpmc",listBox.SelectedItem.ToString());
    con.Open();
    SqlDataReader read = cmd.ExcuteReader();
    if(read.Read()) 
    {
      TextBox1.Text = read[0].ToString();
      TextBox2.Text = read[1].ToString();
    }
    }你的三个字段组和起来成主键 那么有下面的情况cpmc,id,cpbh
    1      1  1
    1       1  2 
    1        1  3 
    这样的时候你想怎么显示呢?
    id,cpbh 也是不是该放在ListBox里面,全部显示?
    或者 把TextBox设置为多行TextBox,然后全部显示。
      

  2.   

    listbox绑定大tatableListBox1.DisplayMember = "cpmc"
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  3.   

    好象不是我所要的结果哦
    我现在自己做出来了
    private void listbox_selectedIndexChanged(object sender, EventArgs e) 

    SqlConnection cn = new SqlConnection(“server=.;database=cpxs;integrated security=sspi”); 
    SqlDataAdapter da = new SqlDataAdapter("Select * from cp where cpmc='"+listbox1.selectvalue+"'",cn); 
    DateSet ds=new DateSet();
    da.fill(ds,"ss");
    for(int i=0;i<ds.tables[0].rows.count;i++)
    {textbox2.text=listbox1.selectedvalue.tosting();
    textbox1.text=ds.tables[0].rows[i][0].tostring();}