When press enter in cell while editing move to next cell and i have code to do that but is not work in last row.
I set allowUserToAddRow = false
private DataGridViewCell dgvEndEditCell;
private bool _EnterMoveNext = true;
private bool isMovedByEnter { set; get; }
[System.ComponentModel.DefaultValue(true)]
public bool OnEnterKeyMoveNext
{
get
{
return this._EnterMoveNext;
}
set
{
this._EnterMoveNext = value;
}
}
private void dataGridView1_SelectionChanged(object sender, System.EventArgs e)
{
if (this._EnterMoveNext && MouseButtons == 0)
{
if (this.dgvEndEditCell != null && dgvTranDetail.CurrentCell != null)
{
if (dgvTranDetail.CurrentCell.RowIndex == this.dgvEndEditCell.RowIndex + 1 && dgvTranDetail.CurrentCell.ColumnIndex == this.dgvEndEditCell.ColumnIndex)
{
int iColNew;
int iRowNew;
if (this.dgvEndEditCell.ColumnIndex >= dgvTranDetail.ColumnCount - 1)
{
iColNew = 0;
iRowNew = dgvTranDetail.CurrentCell.RowIndex;
}
else
{
iColNew = this.dgvEndEditCell.ColumnIndex + 1;
iRowNew = this.dgvEndEditCell.RowIndex;
}
NextCell(iColNew, iRowNew);
}
}
this.dgvEndEditCell = null;
}
}
public void NextCell(int col, int row)
{
int count = 0;
for(int i = 0; i <= dgvTranDetail.ColumnCount - 1; i++)
{
if(dgvTranDetail.Columns[i].Visible == true)
{
count++;
}
}
int colindex = col;
if (colindex < count)
{
if (row == -1 || colindex == -1)
return;
DataGridViewCell cell = dgvTranDetail[colindex, row];
if (cell.Visible == true)
{
dgvTranDetail.CurrentCell = cell;
}
else
{
NextCell(colindex, row);
}
}
else
{
if(dgvTranDetail.Rows.Count - 1 > row)
{
NextCell(0, row +1 );
}
}
isMovedByEnter = true;
}
private void dtTransGrid_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
int iColumn = dgvTranDetail.CurrentCell.ColumnIndex;
int iRow = dgvTranDetail.CurrentCell.RowIndex;
if (iColumn == dgvTranDetail.Columns.Count - 1 && iRow != dgvTranDetail.Rows.Count - 1)
{
dgvTranDetail.CurrentCell = dgvTranDetail[0, iRow + 1];
}
else if (iColumn == dgvTranDetail.Columns.Count - 1 && iRow == dgvTranDetail.Rows.Count - 1)
{
(dgvTranDetail.DataSource as DataTable).Rows.Add("", "", "0.000", "0.000", "", 0, 0, 0, 0, false);
dgvTranDetail.CurrentCell = dgvTranDetail[0, iRow + 1];
}
else
{
dgvTranDetail.CurrentCell = dgvTranDetail[iColumn + 1, iRow];
}
}
}
private void dtTransGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
this.dgvEndEditCell = dgvTranDetail[e.ColumnIndex, e.RowIndex];
}