title: Adding bound combobox to datagridview
body: <p>The scenario is almost the same as <a href="http://arsalantamiz.blogspot.com/2008/09/binding-datagridview-combobox-column.html" rel="nofollow">http://arsalantamiz.blogspot.com/2008/09/binding-datagridview-combobox-column.html</a>. but I can't get it working on c#...</p>
  
  <p>I have mySql db with two tables:
  1. protocols
  2. pcapdata</p>
  
  <p>In protocols tables I have a two fields: idprotocols and protocolName</p>
  
  <p>Int the pcaps table I have wizardProtocol (which is "linked" to the idprotocols field) </p>
  
  <p>What I'm trying to get is to have a combobox containing names which will replace the wizardprotocol field. Next, If the user updates the "names" combobox the wizardProtocol  will be changed accordingly (so I will be able to update the changes in the database accordingly).</p>
  
  <p>Now, after reading some information on the net: I've written the following code:</p>
  
  <pre><code>  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());
          }
      }
  </code></pre>
  
  <p>I'm trying to manipulable the DisplayMember property, but I fail (I know that the problem is with probably with my data-binding, but I can't figure it out...)</p>
  
  <p><strong>UPDATE:</strong> Thanks to the answer, I'm re-attaching the fixed code </p>
  
  <pre><code>            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); 
  </code></pre>