I have a DataGridView with multiple columns and the first column type is Combobox.
I'm trying to focus on the next (2nd) cell when the combobox selection is changed (by mouseclick or by pressing a key)
I actually did it but the problem is exactly what the topic says: the combobox item inside the cell loses its value if I interfere with the .CurrentCell in order to focus to next cell.
It only keeps its value if I press <Enter> or <Tab> key after changing the combobox value
DataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(DataGridView1_EditingControlShowing);
private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.CellStyle.BackColor = Color.Red;
e.CellStyle.ForeColor = Color.White;
if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
{
Console.WriteLine("adding handler");
ComboBox cmb = (ComboBox)e.Control;
//cmb.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
//cmb.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
cmb.SelectionChangeCommitted -= new EventHandler(cmb_SelectionChangeCommitted);
cmb.SelectionChangeCommitted += new EventHandler(cmb_SelectionChangeCommitted);
}
}
void cmb_SelectionChangeCommitted(object sender, EventArgs e)
{
ComboBox cmb = (ComboBox)sender;
Console.WriteLine("Index #{0} Value:{1}", cmb.SelectedIndex.ToString(), cmb.SelectedValue.ToString()); //its working
DataGridViewCell c = DataGridView1.CurrentCell; //get current cell
DataGridView1.CurrentCell = DataGridView1[1, c.RowIndex]; //skips to next cell on the same row
//here combobox lost its value after focusing on another cell!
}