2010-01-28 82 views
0

我的Windows窗體有一個ADD按鈕,每次單擊後都會在窗體中添加一個組合框。問題是,我無法在運行時將它綁定到表列。使用現有的數據綁定源在所有組合框中選擇相同的值。我在這裏編碼在C#如何在Windows窗體(C#)中動態添加組合框並將其綁定到sql數據庫中的表的列

是示例代碼:如果您創建的clickevent一個新的局部變量的組合框

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); 
+0

你有一個代碼示例。 – Kieran 2010-01-28 06:33:28

+0

看起來你已經得到了答案的第一部分。對於第二部分,它應該相當容易實施。讓我們看看你到目前爲止... – tzup 2010-01-28 06:37:14

+0

嘿tzup 問題是與ocbNext.DataSource = this。dummysubjectBindingSource; 它與我爲靜態存在的組合框添加的相同的數據綁定源,當我使用動態添加的cmbo框相同的數據綁定源時,問題就開始了。 初始數據綁定源通過嚮導添加...我不知道如何動態添加一個 – 2010-01-28 07:07:08

回答

5

我加了一個數據集的解決方案和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; 
    } 

這是非常接近你所需要的,或容易適應,以滿足您的期望。享受並請選擇一個答案作爲接受的答案。謝謝!

+0

thnx tzup ...它真的很簡單,它它hlpd。 Cn請你讓我知道mk肯定用戶爲每個組合框選擇一個不同的值?我的意思是有沒有辦法禁用已經選定的值? – 2010-01-28 14:19:13

+0

嗨Raghav,請參閱我的答案的第二部分。 – tzup 2010-01-29 08:24:37

0

應該罰款。如果您爲ComboBox使用全局變量,這可能會解釋您的問題。但是,如果沒有一個樣品你是怎麼做的,很難看到什麼是真正發生的事情,所以覺得這只是一個粗略的猜測

1

選項1:與串填充組合框:

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

選項2:用一個字符串數組填充組合框:

this.comboBox1.Items.AddRange(new object[] { "Syed", "Baqar" }); 
相關問題