Home > Blockchain >  how to use enter key after cell edit in DataGridView c#
how to use enter key after cell edit in DataGridView c#

Time:02-12

im trying to go next cell in datagridview after typing in the cell but it go to next row in same Column ** if not typing any thing in cell it working fine how can solve that

private void dgvacc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                
                int row = dgvacc.CurrentCell.RowIndex;
                int col = dgvacc.CurrentCell.ColumnIndex;


                try
                {
                    if (col < dgvacc.Columns.Count - 1)
                    {
                        dgvacc.CurrentCell = dgvacc.Rows[row -1].Cells[col   1];
                        dgvacc.Focus();
                    }
                    else if (col == dgvacc.Columns.Count - 1)
                    {

                        dgvacc.CurrentCell = dgvacc.Rows[row].Cells[0];
                        dgvacc.Focus();
                    }
                }
                catch (Exception)
                {

                }
            }



        }


CodePudding user response:

Option 1

Override the Form's ProcessCmdKey to trap the Keys.Enter presses and select the next cell IF the gird is focused OR the DataGridView.EditingControl property is not null/Nothing.

public partial class YourForm : Form
{
    public YourForm()
    {
        InitializeComponent();
    }     

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if ((dataGridView1.Focused || dataGridView1.EditingControl != null)
            && keyData == Keys.Enter)
        {
            SelectNextCell();
            return true;
        }
        else
            return base.ProcessCmdKey(ref msg, keyData);
    }

    private void SelectNextCell()
    {
        var col = dataGridView1.CurrentCell.ColumnIndex;
        var row = dataGridView1.CurrentCell.RowIndex;

        if (col < dataGridView1.ColumnCount - 1) col  ;
        else
        {
            col = 0;
            row  ;
        }

        // Optional, select the first cell if the current cell is the last one.
        // You can return; here instead or add new row.
        if (row == dataGridView1.RowCount) row = 0;

        dataGridView1.CurrentCell = dataGridView1[col, row];
    }
}

Option 2

Derive a new class from DataGridView and do the same except the focus and EditingControl checks:

[DesignerCategory("Code")]
public class DataGridViewEx : DataGridView
{
    public DataGridViewEx() : base() { }

    // Optional to enable/disable this feature...
    public bool SelectNextCellOnEnter { get; set; } = true;

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (SelectNextCellOnEnter && keyData == Keys.Enter)
        {
            SelectNextCell();
            return true;
        }
        else
        {
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }

    private void SelectNextCell()
    {
        var col = CurrentCell.ColumnIndex;
        var row = CurrentCell.RowIndex;

        if (col < ColumnCount - 1) col  ;
        else
        {
            col = 0;
            row  ;
        }

        if (row == RowCount) row = 0;

        CurrentCell = this[col, row];
    }
}

CodePudding user response:

thanks for all i found the the answer and it solve it - thanks again

private DataGridViewCell dgvEndEditCell;
        private bool _EnterMoveNext = true;

        [System.ComponentModel.DefaultValue(true)]
        public bool OnEnterKeyMoveNext
        {
            get
            {
                return this._EnterMoveNext;
            }
            set
            {
                this._EnterMoveNext = value;
            }
        }


private void dgvacc_KeyDown(object sender, KeyEventArgs e)
        {
            

            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                int iColumn = dgvacc.CurrentCell.ColumnIndex;
                int iRow = dgvacc.CurrentCell.RowIndex;
                if (iColumn == dgvacc.Columns.Count - 1 && iRow != dgvacc.Rows.Count - 1)
                {
                    dgvacc.CurrentCell = dgvacc[0, iRow   1];
                }
                else if (iColumn == dgvacc.Columns.Count - 1 && iRow == dgvacc.Rows.Count - 1)
                {
                }
                else
                {
                    dgvacc.CurrentCell = dgvacc[iColumn   1, iRow];
                }
            }


        }



private void dgvacc_SelectionChanged(object sender, EventArgs e)
        {
            if (this._EnterMoveNext && MouseButtons == 0)
            {
                if (this.dgvEndEditCell != null && dgvacc.CurrentCell != null)
                {
                    if (dgvacc.CurrentCell.RowIndex == this.dgvEndEditCell.RowIndex   1
                        && dgvacc.CurrentCell.ColumnIndex == this.dgvEndEditCell.ColumnIndex)
                    {
                        int iColNew;
                        int iRowNew;
                        if (this.dgvEndEditCell.ColumnIndex >= dgvacc.ColumnCount - 1)
                        {
                            iColNew = 0;
                            iRowNew = dgvacc.CurrentCell.RowIndex;
                        }
                        else
                        {
                            iColNew = this.dgvEndEditCell.ColumnIndex   1;
                            iRowNew = this.dgvEndEditCell.RowIndex;
                        }
                        dgvacc.CurrentCell = dgvacc[iColNew, iRowNew];
                    }
                }
                this.dgvEndEditCell = null;
            }

        }



  • Related