[Solved] Move to next cell on DataGridView not working when allowUserToAddRow is false in Windows Application

Last Reply 12 days ago By dharmendr

Posted 12 days ago

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];
}

 

You are viewing reply posted by: dharmendr 12 days ago.
Posted 12 days ago
tex says:
 private void dtTransGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
 
 
            this.dgvEndEditCell = dgvTranDetail[e.ColumnIndex, e.RowIndex];
}

Change above code with below.

C#

private void dataGridView1_CellEndEdit(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
    this.dgvEndEditCell = dataGridView1[e.ColumnIndex, e.RowIndex];
    if (dataGridView1.Rows.Count - 1 == e.RowIndex && e.ColumnIndex != dataGridView1.Columns.Count - 1)
    {
        dataGridView1.CurrentCell = dataGridView1[e.ColumnIndex + 1, e.RowIndex];
    }
}

VB.Net

Private Sub dataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dataGridView1.CellEndEdit
    Me.dgvEndEditCell = dataGridView1(e.ColumnIndex, e.RowIndex)
    If dataGridView1.Rows.Count - 1 = e.RowIndex AndAlso e.ColumnIndex <> dataGridView1.Columns.Count - 1 Then
        dataGridView1.CurrentCell = dataGridView1(e.ColumnIndex + 1, e.RowIndex)
    End If
End Sub