I have a DataGridView control, whose first column is DataGridViewComboBoxColumn. Combox element has values let's say: "Custom","AAA","BBB". Second DataGridView column is just editable cells. When user selects any combobox item (except "Custom") user input is moved to the second column cell so he can write text.
What I want to achieve is that when user selects "Custom" then ComboBox value is editable so user can enter own value. I have tried using "OnCurrentCellDirtyStateChanged" and "EditingControlShowing" but this does not work. By "does not work" I mean that it actually sets this combobox to ComboBoxStyle.DropDown and I can edit this combobox item text only after I leave focus from the DataGridView row and then click with mouse on that combobox. But I need that it can be editable already after "Custom" is selected.
public void typeColumnDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
{
if (((ComboBox)e.Control).SelectedIndex == 0)
{
((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
}
}
}
public void typeColumnDataGridView_OnCurrentCellDirtyStateChanged(object sender, EventArgs e)
{
DataGridView dataGridView = sender as DataGridView;
if (dataGridView == null || dataGridView.CurrentCell.ColumnIndex != 0) return;
var dataGridViewComboBoxCell = dataGridView.CurrentCell as DataGridViewComboBoxCell;
if (dataGridViewComboBoxCell != null)
{
if (dataGridViewComboBoxCell.FormattedValue != null)
{
if (dataGridViewComboBoxCell.FormattedValue.ToString() == "Custom")
{
dataGridView.CurrentCell = dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[0];
dataGridView.BeginEdit(true); //This does not work
return;
}
else if (dataGridViewComboBoxCell.FormattedValue.ToString() == "")
{
return;
}
}
}
dataGridView.CurrentCell = dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[1];
dataGridView.BeginEdit(true);
}
Custom
- TextBoxColumn become editable, if not then value of Selected value of ComboBoxColumn copied to the TextBoxColumn. – Fabio