There is a DataGridView, and one column is Datagridviewcomboboxcolumn. With dynamic binding, when binding data. We also bind the data to this column
Write code in the Rowsadded event of DataGridView
<summary>///Add new Line///</summary>// <param name= "sender" ></param> // <param name= "E" ></param> private void dgvgoodstype_rowsadded (object sender, DataGridViewRowsAddedEventArgs e) { DataTable dt = DAL. Sqlhelper.adapter ("Select type from OrderType"); The "T_ category" Here is the Datagridviewcomboboxcolumn column name. datagridviewcomboboxcolumn CBB = dgvgoodstype.columns["T_ Classification"] as Datagridviewcomboboxcolumn; if (CBB! = null) { CBB. Defaultcellstyle.nullvalue = lbtype.text;//Displays the default values to change CBB to suit your needs . DataPropertyName = "type"; Cbb. ValueMember = "type"; Cbb. DisplayMember = "type"; Cbb. DataSource = dt; } }
Here the Datagridviewcomboboxcolumn default display drop-down box, select the need to click three times, the first time to select the cell, the second time to enable editing, the third time to open the drop-down box. If you need one click to open the drop-down box, you can enable the DataGridView Cellenter () event with the following code:
private void Dgvgoodstype_cellenter (object sender, DataGridViewCellEventArgs e) { //implementation Click once to display the drop-down list box if ( Dgvgoodstype.columns[e.columnindex] is datagridviewcomboboxcolumn && e.rowindex! = 1) { Sendkeys.send ("{F4}"); } }
Next we add a selection event to Datagridviewcomboboxcolumn. When we choose. Triggers the event. It needs to be written in the Editingcontrolshowing event. As follows:
ComboBox CBO = new ComboBox (); private void Dgvgoodstype_editingcontrolshowing (object sender, Datagridvieweditingcontrolshowingeventargs e) { Determine the corresponding column//dgvgoodstype.tag = DgvGoodsType.CurrentCell.RowIndex; Save index value if (dgvGoodsType.CurrentCell.OwningColumn.Name = = "T_ category" && DgvGoodsType.CurrentCell.RowIndex!) =-1) {CBO = E.control as ComboBox;//Save current event source. In order to trigger the event. In canceling the CBO. SelectedIndexChanged + = new EventHandler (cbo_selectedindexchanged); } }
void Cbo_selectedindexchanged (object sender, EventArgs e) {combobox Combox = sender as ComboBox; It can be written here. After triggering is the logical code MessageBox.Show (Combox. Text); Combox. The text and Cbo.text get the same value//finish processing, the dynamic event must be revoked. What problems you will encounter if you do not undo. You can try the CBO on your own. SelectedIndexChanged-= new EventHandler (cbo_selectedindexchanged); or Combox. SelectedIndexChanged-= new EventHandler (cbo_selectedindexchanged);}
As said above. If you do not cancel the SelectedIndexChanged event. When you trigger a ComboBox. After. You are clicking the second time. Will trigger the previous event. When the current event is triggered
And so on. Triggering events can be a lot.
WinForm dataGridView datagridviewcomboboxcolumn drop-down box event