0

例如:我动态生成两个组合框box1和box2(在运行时单击添加按钮),并且在box1的选定索引更改时,box2中的项目应该更改;两个框中的数据都是从数据库中获取的。

      int cnt = 0;
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.BusinessUltra1_2ConnectionString"].ConnectionString);
    SqlConnection conb = new SqlConnection(ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.BusinessUltra1_2ConnectionString"].ConnectionString);
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.BusinessUltra1_2ConnectionString"].ConnectionString);
    SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.BusinessUltra1_2ConnectionString"].ConnectionString);
    SqlConnection con3 = new SqlConnection(ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.BusinessUltra1_2ConnectionString"].ConnectionString);

    public Form2()
    {
        InitializeComponent();
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        cnt++;
        AddNewComboBox();
        AddNewComboBox1();
    }

    private void AddNewComboBox()
    {
        ComboBox myNewComboBox = new ComboBox();
        myNewComboBox.Name = "ComboBox1" + cnt.ToString();
        con.Open();
        SqlDataAdapter adp = new SqlDataAdapter("select * from company", con);
        DataSet ds = new DataSet();
        adp.Fill(ds, "company");
        myNewComboBox.DataSource = ds.Tables["company"];
        myNewComboBox.DisplayMember = ds.Tables["company"].Columns[0].ToString();
        myNewComboBox.ValueMember = ds.Tables["company"].Columns[0].ToString();
        //Program.counteritems = myNewComboBox.SelectedValue.ToString();
        myNewComboBox.SelectedIndexChanged += new EventHandler(myNewComboBox_SelectedIndexChanged);

        flowLayoutPanel1.Controls.Add(myNewComboBox);

        con.Close();


    }



    private void AddNewComboBox1()
    {
       //string xyz = Program.counteritems;
        ComboBox myNewComboBox1 = new ComboBox();
        myNewComboBox1.Name = "ComboBox2" + cnt.ToString();
        conb.Open();
        SqlDataAdapter adp1 = new SqlDataAdapter("select * from company", con);
        DataSet ds1 = new DataSet();
        adp1.Fill(ds1, "company");
        myNewComboBox1.DataSource = ds1.Tables["company"];
        myNewComboBox1.DisplayMember = ds1.Tables["company"].Columns[1].ToString();
        myNewComboBox1.ValueMember = ds1.Tables["company"].Columns[1].ToString();
        //myNewComboBox_SelectedIndexChanged(sender);

        myNewComboBox1.SelectedIndexChanged += new EventHandler(myNewComboBox1_SelectedIndexChanged);

        flowLayoutPanel2.Controls.Add(myNewComboBox1);
        //changefunction();
        conb.Close();

    }
    void myNewComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {

        var cbox1 = sender as ComboBox;
        if (cbox1 != null)
        {
            if (cbox1.Name == "ComboBox1" + cnt.ToString())
            {
                var cbox2 = flowLayoutPanel2.Controls.OfType<ComboBox>().Where(c => c.Name == "ComboBox2" + cnt.ToString()).FirstOrDefault();
                cbox2.SelectedValue = cbox1.SelectedValue.ToString();

                con2.Open();
                SqlDataAdapter adfgtyu = new SqlDataAdapter("select *  from Cat_Comp_Item where (Category_Name='" + cbox1.SelectedText + "') ", con2);
                DataSet dsft = new DataSet();
                adfgtyu.Fill(dsft, "Cat_Comp_Item");
                cbox2.DataSource = dsft.Tables["Cat_Comp_Item"];
                cbox2.DisplayMember = dsft.Tables["Cat_Comp_Item"].Columns[1].ToString();

                con2.Close();
           }
        } 

         //string combochange1 = ((ComboBox)sender).Text;
        //if (!string.IsNullOrEmpty(myNewComboBox.SelectedValue.ToString()))
        //{
        //    myNewComboBox1.SelectedValue = myNewComboBox.SelectedValue.ToString();
        //}


     }

    private void Form2_Load(object sender, EventArgs e)
    {

        AddNewComboBox();
        AddNewComboBox1();
    }


    void myNewComboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        MessageBox.Show("Press OK to select this ");
    }
4

2 回答 2

1

我认为您应该绑定第一个组合框值和第二个组合框在添加按钮上添加文本(---select---),并在第一个组合框索引更改上绑定第二个组合框

于 2013-08-29T05:37:21.537 回答
0

您可以执行以下操作

    private void myNewComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        var cbox1 = sender as ComboBox;
        if (cbox1 != null)
        {
            if (cbox1.Name == "ComboBox1")
            {
               var cbox2 = flowLayoutPanel2.Controls.OfType<ComboBox>().Where(c => c.Name == "ComboBox2").FirstOrDefault();
               cbox2.SelectedValue = cbox1.SelectedValue.ToString();
            }
        } 
    }

为此,您需要在公司表中设置comboBox2.ValueMembercomboBox1.ValueMember作为同一列。为此选择 ID 列或主键列。

并且还将您的组合框命名为ComboBox1ComboBox2您创建它时,如下所示

ComboBox myNewComboBox = new ComboBox();
myNewComboBox.Name = "ComboBox1";

并为 ComboBox2 做同样的事情

于 2013-08-29T05:47:03.340 回答