Skip to main content

Default behavior of a DataGridViewComboBoxCell is that it doesn't support typing into the cell.

But you could have a request from the client who wants to have enabled typing in a Combobox cell. In order to achieve this, you need to perform two things:

- the DropDownStype property of the ComboBox editing control needs to be set to DropDown. This will enable typing in the combobox.

- ensure that the value that the user typed into the cell is added to the combo box items collection. A combo box cells value has to be in the items collection, otherwise a DataError event will be fired, so that's the reason why you need to perform this step.

- Advertisement -
- Advertisement -

You can add the value to the items collection could be done in the CellValidating event handler.

private void dataGridView1_CellValidating(object sender,
     DataGridViewCellValidatingEventArgs e)
{
    if (e.ColumnIndex == comboBoxColumn.DisplayIndex)
    {
        if (!this.comboBoxColumn.Items.Contains(e.FormattedValue))
        {
            this.comboBoxColumn.Items.Add(e.FormattedValue);
        }
    }
}
 
private void dataGridView1_EditingControlShowing(object sender,
    DataGridViewEditingControlShowingEventArgs e)
{
    if (this.dataGridView1.CurrentCellAddress.X == comboBoxColumn.DisplayIndex)
    {
        ComboBox cb = e.Control as ComboBox;
        if (cb != null)
        {
            cb.DropDownStyle = ComboBoxStyle.DropDown;
         }
    }
}

- Advertisement -