我想动态生成两个组合框,并希望同时将它们绑定在一起

本文关键字:希望 在一起 绑定 组合 动态 两个 | 更新日期: 2023-09-27 18:29:37

例如:我动态生成两个组合框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 ");
    }

我想动态生成两个组合框,并希望同时将它们绑定在一起

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

您可以执行以下

    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 执行相同操作