按下Enter键时如何将dataGridView光标移至下一行

时间:2018-06-20 06:01:07

标签: c#

我已经为“用户按下Enter时转到下一个单元格”编写了以下代码,但是该代码无法正常工作,并且找不到错误。

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Enter)
        {
            int col = dataGridView1.CurrentCell.ColumnIndex;
            int row = dataGridView1.CurrentCell.RowIndex;

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

            if (row == dataGridView1.RowCount)
            {
                dataGridView1.Rows.Add();
                dataGridView1.CurrentCell = dataGridView1[col, row];

                e.Handled = true;
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

过去,我发现实现这种行为的最佳方法是创建一个自DataGridView继承并覆盖ProcessCmdKey函数的自定义控件。

public class MyDataGridViewControl : DataGridView
{
    protected override Boolean ProcessCmdKey(ref Message msg, Keys keyData)
    {
        Boolean handled = false;

        if ((keyData == Keys.Enter || keyData == Keys.Return))
        {
            handled = NavigateToNextCell();
        }

        if (!handled)
        {
            handled = base.ProcessCmdKey(ref msg, keyData);
        }

        return handled;
    }

    private Boolean NavigateToNextCell()
    {
        Boolean retVal = false;

        if (CurrentCell != null)
        {
            Int32 columnIndex = CurrentCell.ColumnIndex;
            Int32 rowIndex = CurrentCell.RowIndex;

            DataGridViewCell targetCell = null;

            do
            {
                if (columnIndex >= Columns.Count - 1)
                {
                    // Move to the start of the next row
                    columnIndex = 0;
                    rowIndex = rowIndex + 1;
                }
                else
                {
                    // Move to the next cell on the right
                    columnIndex = columnIndex + 1;
                }

                if (rowIndex >= RowCount)
                {
                    break;
                }
                else
                {
                    targetCell = this[columnIndex, rowIndex];
                }
            } while (targetCell.Visible == false);


            if (targetCell != null)
            {
                CurrentCell = targetCell;
            }

            retVal = true;
        }

        return retVal;
    }
}

答案 1 :(得分:0)

我已经解决了问题。现在可以正常工作了.....

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Enter)
        {
            int row = dataGridView1.CurrentCell.RowIndex;
            int colIndex = dataGridView1.CurrentCell.ColumnIndex;

            if (colIndex < dataGridView1.Columns.Count - 1)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[row].Cells[colIndex + 1];
                dataGridView1.Focus();
            }
            else if (colIndex == dataGridView1.Columns.Count - 1)
            {
                dataGridView1.Rows.Add(1);
                dataGridView1.CurrentCell = dataGridView1.Rows[row].Cells[0];
                dataGridView1.Focus();
            }

        }