First:
Data-bound ComboBox control
Drag a ComboBox control on the form design first, and then set the items in the items you want to drop, or bind from a table in the database, this is an estimate.
Second:
Add a drop-down list box to the DataGridView control, which is written after binding DataGridView. In the form's Load method, add: G_DataGridView.Controls.Add (G_combobox); Adds a ComBoBox control to the DataGridView control
Third: Write the following code in the CurrentCellChanged method of the DataGridView control:
DataGridViewCell Currnetcell = G_view.currentcell;
if (Currnetcell! = null && CurrnetCell.OwningColumn.Name = = "Column name")
{
Rectangle tmprect = G_datagridview.getcelldisplayrectangle (Currnetcell.columnindex, Currnetcell.rowindex, true);
G_combobox.text = CurrnetCell.Value.ToString ();
G_combobox.size = tmprect.size;
G_combobox.top = Tmprect.top;
G_combobox.left = Tmprect.left;
G_combobox.visible = true;
}
Else
{
G_combobox.visible = false;
}
pay special attention to this sentence: G_combobox.text = CurrnetCell.Value.ToString ();
If the ComboBox is bound to two values, that is, the encoding and the name, then change the sentence to
g_combobox.selectedvalue= CurrnetCell.Value.ToString () Otherwise, the default value after clicking the ComboBox control is not the value selected in the current table.
Fourth:
Write in the SelectedIndexChanged method of the ComboBox control:
G_DataGridView.CurrentCell.Value = G_combobox.text;
Note: If a code is changed to:
G_DataGridView.CurrentCell.Value = G_ComBoBox.SelectedValue.ToString ();
All right, it's done.
From: http://blog.csdn.net/hfzsjz/article/details/2836979
The final Solution for adding a ComboBox in WinForm DataGridView (click ComboBox to display the contents of the current row by default)