AS mentioned in
How to Create Editable ComboBox in DataGridView, there is no property to enable this for a DataGridView ComboBox. You need to handle the EditingControlShowing event in order to change the DropDownStyle of the ComboBox control to DropDown. Then in the CellValidating event, add the FormattedValue value to the ComboBox list if it doesn't already exist. Note that this method only works when when the DataSource property is not set. Otherwise it will fire the exception"Items collection cannot be modified when the DataSource property is set."
Code: private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
{
DataGridViewComboBoxEditingControl combo = e.Control as DataGridViewComboBoxEditingControl;
combo.DropDownStyle = ComboBoxStyle.DropDown;
combo.TextChanged += new EventHandler(combo_TextChanged);
}
}
Code: void combo _TextChanged(object sender, EventArgs e)
{
this.dataGridView1.NotifyCurrentCellDirty(true);
}
Code: private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == myColumn.Index)
{
object eFormattedValue = e.FormattedValue;
if (!myColumn.Items.Contains(eFormattedValue))
{
myColumn.Items.Add(eFormattedValue);
}
}
}