比如我有一个表.
name1 name2 xx  3个列
我的意思是 用2个combobox来绑定 name1 name2的数据,把列内的数据作为combobox的item.我现在希望是  当选定了 combobox1内的数据,那么combobox2之显示和前面有关联的item,谢谢~~~

解决方案 »

  1.   

    combobox1增加SelectChange事件。
    combobox2.SelectValue=combobox1.SelectValue;数据绑定的时候,都用表的主键作为Value;
    在你的表中估计是XX(第三列)
      

  2.   

    我的意思是先要在combobox把列1的元素先显示出来,才能选择啊
    我绑定了,,那一列..
    怎么运行就只显示一行呢?下拉也没有用.
    能否详细点呢? 很急.,.
      

  3.   

    for example: private void Form1_Load(object sender, EventArgs e)
            {
                DataBind();
            }        private void DataBind()
            {
                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 = "sno";
                this.comboBox2.DataSource = ds.Tables["student"];
                this.comboBox2.DisplayMember = "sname";
                this.comboBox2.ValueMember = "sno";
            }
      

  4.   

    access的数据库..谢谢,c#不是太熟
      

  5.   

    在comboBox1_SelectedIndexChanged事件里面, 调用
    select comboBox1.SelectedItem.ToString() from name2又或者用Hashtable将name1当作key, name2中的内容序列号当作Hashtable的value, 然后在comboBox1_SelectedIndexChanged事件里面调用
    comboBox2.SelectedIndex = (int)hashtable[(string)comboBox1.SelectedItem];
      

  6.   

    private void Form1_Load(object sender, EventArgs e)
            {
                DataBind();
            }        private void DataBind()
            {
                OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=d:\student.mdb;");
                OleDbDataAdapter sda = new OleDbDataAdapter("select * from studentDetails", con);
                DataSet ds = new DataSet();
                sda.Fill(ds, "student");
                this.comboBox1.DataSource = ds.Tables["student"];
                this.comboBox1.DisplayMember = "sno";
                this.comboBox2.DataSource = ds.Tables["student"];
                this.comboBox2.DisplayMember = "sname";
                this.comboBox2.ValueMember = "sno";
            }
      

  7.   

    Don't forget:using System.Data.OleDb;
      

  8.   

    谢谢liujia_0421~`
    但是当选定了 combobox1内的数据,那么combobox2之显示和前面有关联的item,谢谢~~~比如 name1 name2 xx
         a      b     1
         a      c     1
         b      d     1
    当我选择了combobox1的 a.. 我希望 combobox2中只显示 b,c
    同时我希望 用一个textbox 来显示 combobox1.text 和combobox2.text 对应的那个xx的值.
    谢谢
      

  9.   

    TO:当我选择了combobox1的 a.. 我希望 combobox2中只显示 b,c
    同时我希望 用一个textbox 来显示 combobox1.text 和combobox2.text 对应的那个xx的值.
    谢谢
    哦,那我把你的意思给理解错了,给你提供个思路吧..我就不帮你写代码了..private void DataBind()
            {
                OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=d:\student.mdb;");
                OleDbDataAdapter sda = new OleDbDataAdapter("select * from studentDetails", con);
                DataSet ds = new DataSet();
                sda.Fill(ds, "student");
                this.comboBox1.DataSource = ds.Tables["student"];
                this.comboBox1.DisplayMember = "sno";
                //把DataBind中的这几句注释掉..
                //this.comboBox2.DataSource = ds.Tables["student"];
                //this.comboBox2.DisplayMember = "sname";
                //this.comboBox2.ValueMember = "sno";
            }然后再ComboBox的selectedIndexChanged事件下...以ComboBox1当前选中的Value再查找数据库,将结果绑定到CommboBox2中即可...
      

  10.   

    呵呵.谢谢,,好了..
    但是textbox怎么和xx这列绑定.
    我的意思是 当combobox1 和combobox2选定后,然后 可以定一个xx的值.
      OleDbDataAdapter sda = new OleDbDataAdapter("select  rates from rates where moneyname1='" + comboBox1.Text + "'&&moneyname2='" + comboBox2.Text + "'", con);???
      

  11.   

    取出来后,直接显示在TextBox中不就OK了?this.TextBox.Text="查询出来的值";
      

  12.   

    能详细点吗?
      OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=rats.mdb;User Id=Admin;Password=;");
                OleDbDataAdapter sda = new OleDbDataAdapter("select  rates from rates where moneyname1='" + comboBox1.Text + "'and moneyname2='" + comboBox2.Text + "'", con);
                DataSet ds = new DataSet();
                sda.Fill(ds, "rate");
                //this.textBox1.DataBindings.Add("Text", sda, "rates.rates");
              //  this.textBox1.Text =;
    该怎么改,谢谢.
      

  13.   

    TO:
    OleDbDataAdapter sda = new OleDbDataAdapter("select  rates from rates where moneyname1='" + comboBox1.Text + "'and moneyname2='" + comboBox2.Text + "'", con);
                DataSet ds = new DataSet();
                sda.Fill(ds, "rate");
                //this.textBox1.DataBindings.Add("Text", sda, "rates.rates");
              //  this.textBox1.Text =;
    这里没有必要再绑定了...
    OleDbCommand cmd=new OleDbCommand("你的查询字符串");
    //打开连接
    con.Open();
    OleDbDataReader sdr=cmd .ExecuteReader();
    if (sdr.Read())
    {
       this.TextBox1.Text=Convert.ToString(sdr["rates"]);
    }
    sdr.Close();
    //关闭数据库连接
    con.Close();
      

  14.   

    我的那个sql语句是不是格式不对?
      

  15.   

    OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;
    Data Source=rats.mdb;User Id=Admin;Password=;");
      OleDbCommand cmd = new OleDbCommand("select  rates from rates where
    moneyname1='" + comboBox1.Text + "'and moneyname2='" + comboBox2.Text + "'");
                //打开连接
                con.Open();
                OleDbDataReader sdr = cmd.ExecuteReader();///报错 Connection 属性尚
    未初始化。
                if (sdr.Read())
                {
                    this.textBox1.Text = Convert.ToString(sdr["rates"]);
                }
                sdr.Close();
                //关闭数据库连接
                con.Close();
      

  16.   

    用Oracle数据库给你写了个例子,看下:private void Form1_Load(object sender, EventArgs e)
            {
                DataBind();
            }
            private void DataBind()
            {
                OleDbConnection con = new OleDbConnection("Provider=OraOLEDB.Oracle.1;data source=oracle;user id=****;password=****");
                OleDbDataAdapter sda = new OleDbDataAdapter("select * from student", con);
                DataSet ds = new DataSet();
                sda.Fill(ds, "student");
                this.comboBox1.DataSource = ds.Tables["student"];
                this.comboBox1.DisplayMember = "sname";
            }        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                OleDbConnection con = new OleDbConnection("Provider=OraOLEDB.Oracle.1;data source=oracle;user id=****;password=****");
                if (this.comboBox1.SelectedItem != null)
                {
                    OleDbCommand cmd = new OleDbCommand("select sage from student where sname='" + this.comboBox1.Text + "'", con);
                    con.Open();
                    OleDbDataReader sdr = cmd.ExecuteReader();
                    if (sdr.Read())
                    {
                        this.textBox1.Text = Convert.ToString(sdr["sage"]);
                    }
                    sdr.Close();
                    con.Close();
                }
            }