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

时间:2013-08-29 05:10:55

标签: c# winforms sql-server-2008

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

2 个答案:

答案 0 :(得分:1)

我认为你应该绑定第一个组合框值,第二个组合框只添加文本(---选择---)添加按钮和第一个组合框索引更改绑定第二个组合框上的字母

答案 1 :(得分: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做同样的事情