简体   繁体   English

按Enter键时,如何将dataGridView光标转到下一行

[英]How to dataGridView cursor go to next line when press Enter

I have written the Below code for "Go to next cell when user press enter" but the code is not working and I am not able to find the error. 我已经编写了下面的代码“当用户按下输入时转到下一个单元格”但代码不起作用,我无法找到错误。

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

In the past I have found that the best way to implement this sort of behaviour is to create a Custom Control that inherits from the DataGridView and override the ProcessCmdKey function. 在过去,我发现实现此类行为的最佳方法是创建一个继承自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;
    }
}

I have solved the problem. 我已经解决了这个问题。 now its working fine..... 现在它的工作正常.....

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();
            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM