如何在按 Enter 时 dataGridView 光标转到下一行

我已经为“当用户按下回车键时转到下一个单元格”编写了下面的代码,但代码不起作用,我无法找到错误。


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;

            }

        }

    }


呼如林
浏览 398回答 2
2回答

翻阅古今

在过去,我发现实现这种行为的最佳方法是创建一个继承自DataGridView并覆盖该ProcessCmdKey函数的自定义控件。public class MyDataGridViewControl : DataGridView{&nbsp; &nbsp; protected override Boolean ProcessCmdKey(ref Message msg, Keys keyData)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Boolean handled = false;&nbsp; &nbsp; &nbsp; &nbsp; if ((keyData == Keys.Enter || keyData == Keys.Return))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handled = NavigateToNextCell();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (!handled)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handled = base.ProcessCmdKey(ref msg, keyData);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return handled;&nbsp; &nbsp; }&nbsp; &nbsp; private Boolean NavigateToNextCell()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Boolean retVal = false;&nbsp; &nbsp; &nbsp; &nbsp; if (CurrentCell != null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Int32 columnIndex = CurrentCell.ColumnIndex;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Int32 rowIndex = CurrentCell.RowIndex;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataGridViewCell targetCell = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (columnIndex >= Columns.Count - 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Move to the start of the next row&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; columnIndex = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowIndex = rowIndex + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Move to the next cell on the right&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; columnIndex = columnIndex + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (rowIndex >= RowCount)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; targetCell = this[columnIndex, rowIndex];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } while (targetCell.Visible == false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (targetCell != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CurrentCell = targetCell;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return retVal;&nbsp; &nbsp; }}

哆啦的时光机

我已经解决了这个问题。现在它工作正常......private void dataGridView1_KeyDown(object sender, KeyEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (e.KeyData == Keys.Enter)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int row = dataGridView1.CurrentCell.RowIndex;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int colIndex = dataGridView1.CurrentCell.ColumnIndex;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (colIndex < dataGridView1.Columns.Count - 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView1.CurrentCell = dataGridView1.Rows[row].Cells[colIndex + 1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView1.Focus();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (colIndex == dataGridView1.Columns.Count - 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView1.Rows.Add(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView1.CurrentCell = dataGridView1.Rows[row].Cells[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView1.Focus();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP