我加了一個數據集的解決方案和DROP掉的員工表(從羅斯文 )在設計師,這自動創建employeesBindingSource。我在窗體上放置了一個組合框和一個按鈕,我設置了組合框的數據源和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);
}
因此,對每一次點擊,一個新的組合體被添加到表單中動態權之前的組合下。該組合也綁定到Employees表中的下一列(不過沒有邊界檢查)。
正如你所看到的,這是很容易的事情。希望這可以幫助。
好了,所以這裏是代碼,可以幫助你與你在這個答案的評論中問道,其他問題的變化。
它假定你有一個按鈕表和數據集與表員工。在按鈕上單擊它創建一個組合,並填充數據(名稱列員工)。每次添加一個組合體時,它都會獲得自己的數據副本(這對於一次能從一個組合體中移除項目非常重要)。然後,每次在組合中選擇一個值時,該組合都被禁用,其他組合在其列表中沒有該選定值。
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;
}
這是非常接近你所需要的,或容易適應,以滿足您的期望。享受並請選擇一個答案作爲接受的答案。謝謝!
你有一個代碼示例。 – Kieran 2010-01-28 06:33:28
看起來你已經得到了答案的第一部分。對於第二部分,它應該相當容易實施。讓我們看看你到目前爲止... – tzup 2010-01-28 06:37:14
嘿tzup 問題是與ocbNext.DataSource = this。dummysubjectBindingSource; 它與我爲靜態存在的組合框添加的相同的數據綁定源,當我使用動態添加的cmbo框相同的數據綁定源時,問題就開始了。 初始數據綁定源通過嚮導添加...我不知道如何動態添加一個 – 2010-01-28 07:07:08