将绑定的combobox添加到datagridview

该场景与http://arsalantamiz.blogspot.com/2008/09/binding-datagridview-combobox-column.html几乎相同。 但我不能让它在c#上工作……

我有两个表的mySql数据库:1。协议2. pcapdata

在协议表中,我有两个字段:idprotocols和protocolName

在pcaps表中我有wizardProtocol(它与idprotocols字段“链接”)

我想要得到的是一个包含名称的combobox,它将取代向导协议字段。 接下来,如果用户更新“名称”combobox,则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属性,但是我失败了(我知道问题可能与我的数据绑定有关,但我无法弄清楚……)

更新:由于答案,我正在重新附加固定代码

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

你做错的最明显的事情是你对datagridview和你的combobox使用相同的绑定源。 如果你看一下你提供的例子,你会发现他们创建了第二个bindingsource productBindingSource。

所以你需要做的是创建一个bindingsource(让我们称之为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); 

上面应该对你有用,虽然我不知道你的数据集列的名称我不得不猜一点。