2011-09-28 68 views
3

該場景與http://arsalantamiz.blogspot.com/2008/09/binding-datagridview-combobox-column.html幾乎相同。但我不能讓它工作在C#...將綁定的組合框添加到datagridview中

我有MySQL數據庫有兩個表: 1.協議 2. pcapdata

在協議表我有兩個領域:idprotocols和protocolName

詮釋了pcaps表我有wizardProtocol(這是「鏈接」到idprotocols場)

我想要得到的是具有包含名稱將取代wizardprotocol場的組合框。接下來,如果用戶更新了「名稱」組合框,wizardProtocol將相應地更改(這樣我就可以相應地更新數據庫中的更改)。

現在,閱讀在網絡上的一些信息後,我已經寫了下面的代碼:

public void Bind(ref DataGridView dataGridView) 
    { 
     try 
     { 

      mySqlDataAdapter = new MySqlDataAdapter(SELECT_ALL_PCAP, _con); 
      mySqlCommandBuilder = new MySqlCommandBuilder(mySqlDataAdapter); 

      mySqlDataAdapter.UpdateCommand = mySqlCommandBuilder.GetUpdateCommand(); 
      mySqlDataAdapter.DeleteCommand = mySqlCommandBuilder.GetDeleteCommand(); 
      mySqlDataAdapter.InsertCommand = mySqlCommandBuilder.GetInsertCommand(); 
      dataSet = new DataSet(); 
      mySqlDataAdapter.Fill(dataSet, "pcap"); 


      MySqlDataAdapter adp2 = new MySqlDataAdapter(SELECT_ALL_PROTOCOL, _con); 
      MySqlCommandBuilder builder = new MySqlCommandBuilder(adp2); 

      adp2.UpdateCommand = builder.GetUpdateCommand(); 
      adp2.DeleteCommand = builder.GetDeleteCommand(); 
      adp2.InsertCommand = builder.GetInsertCommand(); 
      adp2.Fill(dataSet, "protocol"); 


      bindingSource = new BindingSource(); 
      bindingSource.DataSource = dataSet; 
      bindingSource.DataMember = "pcap"; 
      dataGridView.DataSource = bindingSource; 


      dataGridView.Columns["length"].ReadOnly = true; 
      dataGridView.Columns["length"].DefaultCellStyle.ForeColor = System.Drawing.Color.SandyBrown; 
      dataGridView.AllowUserToAddRows = false; 
      dataGridView.AllowUserToDeleteRows = false; 


      DataGridViewComboBoxColumn colType = new DataGridViewComboBoxColumn(); 
      colType.HeaderText = "Type"; 
      colType.DropDownWidth = 90; 
      colType.Width = 90; 
      colType.DataPropertyName = "wizardProtocol"; 
      colType.DataSource = bindingSource; 
      colType.DisplayMember = "protocolName"; 
      colType.ValueMember = "idprotocols"; 
      dataGridView.Columns.Insert(dataGridView.Columns.GetColumnCount(DataGridViewElementStates.None) - 1, colType); 


     } 
     catch (System.Exception e) 
     { 
      MessageBox.Show(e.ToString()); 
     } 
    } 

我試圖操縱DisplayMember屬性,但我失敗了(我知道這個問題是可能與我的數據綁定,但我不出來...)

UPDATE:多虧了答案,我重新安裝了固定碼

  mySqlDataAdapter = new MySqlDataAdapter(SELECT_ALL_PCAP, _con); 
      mySqlCommandBuilder = new MySqlCommandBuilder(mySqlDataAdapter); 

      mySqlDataAdapter.UpdateCommand = mySqlCommandBuilder.GetUpdateCommand(); 
      mySqlDataAdapter.DeleteCommand = mySqlCommandBuilder.GetDeleteCommand(); 
      mySqlDataAdapter.InsertCommand = mySqlCommandBuilder.GetInsertCommand(); 
      dataSet = new DataSet(); 
      mySqlDataAdapter.Fill(dataSet, "pcap"); 


      MySqlDataAdapter adp2 = new MySqlDataAdapter(SELECT_ALL_PROTOCOL, _con); 
      MySqlCommandBuilder builder = new MySqlCommandBuilder(adp2); 

      adp2.UpdateCommand = builder.GetUpdateCommand(); 
      adp2.DeleteCommand = builder.GetDeleteCommand(); 
      adp2.InsertCommand = builder.GetInsertCommand(); 
      adp2.Fill(dataSet, "protocol"); 



      bindingSource = new BindingSource(); 
      bindingSource.DataSource = dataSet; 
      bindingSource.DataMember = "pcap"; 
      dataGridView.DataSource = bindingSource; 
      dataGridView.AllowUserToAddRows = false; 
      dataGridView.AllowUserToDeleteRows = false; 


      DataGridViewComboBoxColumn colType = new DataGridViewComboBoxColumn(); 
      BindingSource wizardBindingSource = new BindingSource(); 
      wizardBindingSource.DataSource = dataSet; 
      wizardBindingSource.DataMember = "protocol"; 
      colType.HeaderText = "Type"; 
      colType.DropDownWidth = 90; 
      colType.Width = 90; 
      colType.DataPropertyName = "wizardProtocol"; 
      colType.DataSource = wizardBindingSource; 
      colType.DisplayMember = "protocolName"; 
      colType.ValueMember = "idprotocols"; 
      dataGridView.Columns.Insert(dataGridView.Columns.GetColumnCount(DataGridViewElementStates.None) - 1, colType); 
+0

我有同樣的問題,你可以請幫助 http://stackoverflow.com/questions/16758407/modifying-a-column-in-dgv-to-a--a-combobox-bound-to-database – user2404633

回答

4

你做錯了最顯而易見的事情是,你爲datagridview和comboboxcolumn使用相同的綁定源。如果你看看你提供的例子,你會注意到他們創建了第二個綁定源productBindingSource。

所以你需要做的是創建一個綁定源(我們稱之爲wizardProtocolBindingSource),然後填充協議表中的數據。這成爲您的ComboBox列的數據源。

關鍵代碼看起來是這樣的:

// You bind the datagridview just as before 
// this dataset should have the idprotocols field which is your foreign key 
// to the protocols table - you will probably want this to be hidden. 
bindingSource = new BindingSource(); 
bindingSource.DataSource = dataSet; 
bindingSource.DataMember = "pcap"; 
dataGridView.DataSource = bindingSource; 

// hide the foreign key column 
dataGridView.Columns["idProtocols"].Visible = false; 

// here we populate your comboboxcolumn binding source 
wizardProtocolBindingSource= new BindingSource(); 
// this dataset is from the protocols table 
wizardProtocolBindingSource.DataSource = dataSet; 

// Add the combobox column 
DataGridViewComboBoxColumn colType = new DataGridViewComboBoxColumn();  
colType.HeaderText = "Type";  
colType.DropDownWidth = 90;  
colType.Width = 90;  
colType.DataSource = wizardProtocolBindingSource;  
// The DataPropertyName refers to the foreign key column on the datagridview datasource 
colType.DataPropertyName = "wizardProtocol";  
// The display member is the name column in the column datasource 
colType.DisplayMember = "protocolName";  
// The value member is the primary key of the protols table 
colType.ValueMember = "idprotocols";  
// I usually just add the column but you can insert if you need a particular position 
dataGridView.Columns.Add(colType);  

以上應該爲你工作,但不知道你的數據集列的名字,我猜了一點。

+0

真的幫助了我,謝謝 –