如何动态地在Windows窗体(C#)添加组合框并绑定到一个表的SQL数据库中的列组合、窗体、数据库中、绑定

2023-09-04 00:51:23 作者:深知他是梦还当他是命

我的窗口形式所具有的ADD按钮增加了一个组合框的形式之后,每一次点击。现在的问题是,我不能够将其绑定到一个表列在运行时。使用现有的数据绑定源选择中的所有组合框相同的值。我编码在C#

下面是示例code:

 组合框ocbNext =新组合框();
//设置的属性的休息权,问题出在数据绑定
ocbNext.DataSource = this.dummysubjectBindingSource;
ocbNext.DisplayMember =sub_name;
ocbNext.ValueMember =sub_name;
this.Controls.Add(ocbNext);
 

解决方案

我添加了一个数据集以解决方案和DROP掉了的员工的表(从罗斯文)的设计师,它会自动创建在 employeesBindingSource 。我放弃了一个组合框和窗体上的一个按钮,我设置组合的数据源和数据成员。然后我处理一些事件:

 私人无效Form1_Load的(对象发件人,EventArgs的)
    {
        this.employeesTableAdapter.Fill(this.dS.Employees);
    }

    私人诠释_i = 0;
    私人无效的button1_Click(对象发件人,EventArgs的)
    {
        组合框组合=新组合框();
        combo.DataSource = this.employeesBindingSource;
        combo.DisplayMember = this.dS.Tables [0] .Columns [++ _我] .ColumnName;
        combo.Location =新的点(comboBox1.Location.X,comboBox1.Location.Y + comboBox1.Height * _i);
        this.Controls.Add(组合);
    }
 
2020 10 27

所以在每次点击,一个新的组合被添加到窗体动态地权下的previous组合。该组合也势必会在雇员表中的下一列(无边界检查不过)。

正如你所看到的,这是pretty的简单的东西。希望这可以帮助。

好了,所以这里的code,可以帮助你与你这个答案的意见提出的其他问题的变化。

它假定你有一个表格有一个按钮和一个的数据集的桌子的员工的。在按钮点击它会创建一个组合,并用数据填充它(的名称的列的员工的)。每次添加一个组合,它获取自己的数据副本(这是为了能够从一个组合在同一时间删除项目很重要)。然后,您可以选择在组合值每一次,组合为残疾人和其他组合没有在他们的列表中选择的值。

 私人INT _i = 0;
    私人无效的button1_Click(对象发件人,EventArgs的)
    {
        数据集DATAS = dS.Clone();
        this.employeesTableAdapter.Fill((DS.EmployeesDataTable)dataS.Tables [0]);
        BindingSource的bindSource =新的BindingSource(DATAS,员工);

        组合框组合=新组合框();
        combo.Name = this.dS.Tables [0] .Columns [0] .ColumnName +(++ _ⅰ)的ToString();
        combo.DataSource = bindSource;
        combo.DisplayMember = this.dS.Tables [0] .Columns [1] .ColumnName; //此列是员工的姓名
        combo.Location =新的点(button1.Location.X,button1.Location.Y + combo.Height * _i);
        combo.SelectedIndexChanged + =新的EventHandler(comboBox_SelectedIndexChanged);
        this.Controls.Add(组合);
    }

    私人无效comboBox_SelectedIndexChanged(对象发件人,EventArgs的)
    {
        的foreach(在this.Controls控制CTRL)
        {
            如果(CTRL为组合框和放大器;&安培;按Ctrl =发件人和放大器;!&安培; ctrl.Enabled)
            {
                ((的BindingSource)((组合框)CTRL).DataSource).RemoveAt(((组合框)发送方).SelectedIndex);
            }
        }
        ((组合框)发送方).Enabled = FALSE;
    }
 

这是pretty的接近您所需要的,还是很容易适应,以满足您的期望。享受和请选择答案为接受的。谢谢!

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 existing databinding source selects the same value in all the combo boxes. I am coding in C#

here is the sample code :

ComboBox ocbNext = new ComboBox();
//HAVE set the rest of the properties right, the problem is with the databinding
ocbNext.DataSource = this.dummysubjectBindingSource;
ocbNext.DisplayMember = "sub_name";
ocbNext.ValueMember = "sub_name";
this.Controls.Add(ocbNext);

解决方案

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!