您的位置:首页 > 其它

combox1有值时combox2显示与combox1有关的值

2016-06-03 10:14 176 查看
private void comboBox2_Click(object sender, EventArgs e)

        {

            if (comboBox1.SelectedItem == null || comboBox1.Text == "-请选择-")

            {

                SqlDataAdapter da1 = new SqlDataAdapter("select distinct Buys.GId,GName from Buys,Goods where Buys.GId = Goods.GId", cn);

                DataSet ds1 = new DataSet();

                //comboBox2.Items.Clear();

                da1.Fill(ds1, "Good");

                DataRow dr1 = ds1.Tables["Good"].NewRow();

                dr1["GId"] = "0";

                dr1["GName"] = "-请选择-";

                ds1.Tables["Good"].Rows.InsertAt(dr1, 0);

                comboBox2.DataSource = ds1.Tables["Good"];

                comboBox2.DisplayMember = "GName";

                comboBox2.ValueMember = "GId";

                cn.Close();

            }

            else

            {

                SqlDataAdapter da2 = new SqlDataAdapter("select distinct Buys.GId,GName from Buys,Goods where Buys.GId = Goods.GId and Buys.WorId = " + comboBox1.SelectedValue.ToString() + "", cn);

                DataSet ds2 = new DataSet();

                //comboBox2.Items.Clear();

                da2.Fill(ds2, "Good");

                DataRow dr2 = ds2.Tables["Good"].NewRow();

                dr2["GId"] = "0";

                dr2["GName"] = "-请选择-";

                ds2.Tables["Good"].Rows.InsertAt(dr2, 0);

                comboBox2.DataSource = ds2.Tables["Good"];

                comboBox2.DisplayMember = "GName";

                comboBox2.ValueMember = "GId";

                cn.Close();

            }  

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: