Column 0 in a DataGridView control is Read Only. I want the focus to move to column 1 if the user selects column 0 with the mouse or presses the tab key from the last column in the previous row. I tried the following code in the CellEnter event but it causes an exception "Operation is not valid because it results in a reentrant call to the CurrentCellAddressCore function". Column 1 is named "patternsTerminator".
private void dataGridView_patterns_CellEnter(object sender, DataGridViewCellEventArgs e)
{
int currentRow = e.RowIndex;
try
{
if (this.dataGridView_patterns.CurrentCell.ColumnIndex == 0)
this.dataGridView_patterns.CurrentCell = this.dataGridView_patterns.Rows[currentRow].Cells["patternsTerminator"];
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, errCaption, button, icon);
}
}
I understand why the exception occurs. When the focus moves to column 1, the CellEnter event is called again, and the exception prevents recursive calls to the CellEnter event.
I tried the following as a workaround but it ignores the tab key. When I click in column 0, SendKeys is called but the cursor stays in column 0.
if (this.dataGridView_patterns.CurrentCell.ColumnIndex == 0)
SendKeys.Send("{TAB}");
I have read many of the threads on this and other websites but can't get it to work. Any suggestions would be greatly appreciated.
Thank you!