i am working on a scoring program for Darts. I use a Datagridview to enter the scores. After pressing enter key the program will check if finished or not.
If not, the program adds another row for scoring, and should set the next Cell to CurrentCell in EditMode.
The row is being added fine, but the CurrentCell is selected and not in edit Mode.
Can anyone help me please? Thanks in advance.
private void DGV_score_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
if (DGV_score.CurrentRow.Index == maxrow)
{
DGV_score.Rows.Add();
maxrow = maxrow + 1;
if (leg % 2 != 0)
{
DGV_score.CurrentCell = DGV_score.Rows[maxrow].Cells[0];
DGV_score.BeginEdit(true);
}
if (leg % 2 == 0)
{
DGV_score.CurrentCell = DGV_score.Rows[maxrow].Cells[2];
}
DGV_score.BeginEdit(true);
}
}
}
I tried calling the BeginEdit in CurrentCellChanged instead, but this also doesn't set the cell in EditMode.
private void DGV_score_CurrentCellChanged(object sender, EventArgs e)
{
DGV_score.BeginEdit(true);
}
I have no idea how to fix this, maybe it collides with the way i handle the enter key (I used a solution suggested at this community?
private void DGV_score_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is DataGridViewTextBoxEditingControl)
{
DataGridViewTextBoxEditingControl tb = e.Control as DataGridViewTextBoxEditingControl;
tb.KeyDown -= DGV_score_KeyDown;
tb.PreviewKeyDown -= DGV_score_PreviewKeyDown;
tb.KeyDown += DGV_score_KeyDown;
tb.PreviewKeyDown += DGV_score_PreviewKeyDown;
}
}