如何在windows forms(C#)中动态添加combobox并将其绑定到sql数据库表的一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2152705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to dynamically add combobox in windows forms(C#) and bound it to a column of a table in sql database
提问by Raghav Bali
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#
我的 windows 窗体有一个添加按钮,每次单击后都会向窗体添加一个组合框。问题是,我无法在运行时将其绑定到表列。使用现有的数据绑定源会在所有组合框中选择相同的值。我正在用 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);
采纳答案by tzup
I added a DataSetto the solution and droped the Employeestable (from Northwind) in the designer, which automatically created the employeesBindingSource. I dropped a combobox and a button on the Form and I set the DataSourceand DataMemberof the combo. Then I handled some events:
我在解决方案中添加了一个DataSet并在设计器中删除了Employees表(来自Northwind),它自动创建了EmployeesBindingSource。我在 Form 上放置了一个组合框和一个按钮,并设置了组合的DataSource和DataMember。然后我处理了一些事件:
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).
因此,在每次单击时,一个新的组合就会动态地添加到表单中,就在上一个组合的正下方。该组合还绑定到Employees 表中的下一列(但是没有边界检查)。
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 Formwith a button and a DataSetwith table Employees. On button click it creates a combo, and fills it with data (the Namecolumn 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.
它假设您有一个带有按钮的Form和一个带有表Employees的DataSet。单击按钮时,它会创建一个组合,并用数据填充它(员工的Name列)。每次添加组合时,它都会获得自己的数据副本(这对于一次从一个组合中删除项目很重要)。然后,每次您在组合中选择一个值时,组合都会被禁用,其他组合在其列表中没有该选定值。
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!
这非常接近您的要求,或者很容易适应以满足您的期望。享受并请选择一个答案作为接受的答案。谢谢!
回答by Sascha
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
如果您在 clickevent 中创建一个新的本地 ComboBox 变量应该没问题。如果您为 ComboBox 使用全局变量,这可能会解释您的问题。但是如果没有样本你是如何做的,就很难看出真正发生了什么,所以认为这只是一个粗略的猜测
回答by Syed Baqar Hassan
Option 1: Fill the combobox with strings:
选项 1:用字符串填充组合框:
this.comboBox1.Items.Add("Syed");
this.comboBox1.Items.Add("Baqar");
Option 2: Fill the combobox with an array of strings:
选项 2:用字符串数组填充组合框:
this.comboBox1.Items.AddRange(new object[] { "Syed", "Baqar" });