how to dynamically add combobox in windows forms(C#) and bound it to a column of a table in sql database

前端 未结 3 1965
我寻月下人不归
我寻月下人不归 2021-01-07 14:48

My windows form has an ADD button which adds a combo box to the form after each click. The problem is, i am not able to bind it to a table column at run time. Using an exist

相关标签:
3条回答
  • 2021-01-07 14:57

    Should be fine if you create a new local ComboBox variable in the clickevent. If you use a global variable for the ComboBox this might explain your problems. But without a sample how you're doing it's hard to see what's really happening, so think this is just a rough guess

    0 讨论(0)
  • 2021-01-07 15:00

    Option 1: Fill the combobox with strings:

    this.comboBox1.Items.Add("Syed");
    this.comboBox1.Items.Add("Baqar");
    

    Option 2: Fill the combobox with an array of strings:

    this.comboBox1.Items.AddRange(new object[] { "Syed", "Baqar" });
    
    0 讨论(0)
  • 2021-01-07 15:04

    I added a DataSet to the solution and droped the Employees table (from Northwind) in the designer, which automatically created the employeesBindingSource. I dropped a combobox and a button on the Form and I set the DataSource and DataMember of the combo. Then I handled some events:

        private void Form1_Load(object sender, EventArgs e)
        {
            this.employeesTableAdapter.Fill(this.dS.Employees);
        }
    
        private int _i = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            ComboBox combo = new ComboBox();
            combo.DataSource = this.employeesBindingSource;
            combo.DisplayMember = this.dS.Tables[0].Columns[++_i].ColumnName;
            combo.Location = new Point(comboBox1.Location.X, comboBox1.Location.Y + comboBox1.Height * _i);
            this.Controls.Add(combo);
        }
    

    So on each click, a new combo is added onto the form dynamically right under the previous combo. The combo is also bound to the next column in the Employees table (no boundary checks however).

    As you can see, this is pretty easy stuff. Hope this helps.


    Okay, so here is a variation of the code that could help you with that other question you asked in the comments of this answer.

    It assumes you have a Form with a button and a DataSet with table Employees. On button click it creates a combo, and fills it with data (the Name column of Employees). Each time you add a combo, it gets its own copy of the data (this is important to be able to remove items from one combo at a time). Then, every time you select a value in the combo, the combo is disabled and the other combos don't have that selected value in their list.

      private int _i = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            DataSet dataS = dS.Clone();
            this.employeesTableAdapter.Fill((DS.EmployeesDataTable)dataS.Tables[0]);
            BindingSource bindSource = new BindingSource(dataS, "Employees");
    
            ComboBox combo = new ComboBox();
            combo.Name = this.dS.Tables[0].Columns[0].ColumnName + (++_i).ToString();
            combo.DataSource = bindSource;
            combo.DisplayMember =  this.dS.Tables[0].Columns[1].ColumnName; //This column is the Name of Employee
            combo.Location = new Point(button1.Location.X, button1.Location.Y + combo.Height * _i);
            combo.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
            this.Controls.Add(combo);
        }
    
        private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl is ComboBox && ctrl != sender && ctrl.Enabled)
                {
                    ((BindingSource)((ComboBox)ctrl).DataSource).RemoveAt(((ComboBox)sender).SelectedIndex);
                }
            }
            ((ComboBox)sender).Enabled = false;
        }
    

    This is pretty close to what you require, or easily adaptable to meet your expectations. Enjoy and please select an answer as the accepted one. Thanks!

    0 讨论(0)
提交回复
热议问题