如何将重点放在datagridview中的下一个单元格中输入按键事件 [英] How to move focus on next cell in a datagridview on Enter key press event

查看:410
本文介绍了如何将重点放在datagridview中的下一个单元格中输入按键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,我正在使用C#编写Windows应用程序。我正在使用datagridview来显示记录。我需要的功能是当我按Enter键时,焦点应该转到下一个单元格(同一列的列)。如果它是网格中的最后一列,那么焦点应该转到下一行的第一列。我已经尝试使用

Friends, I'm working on windows application using C#. I'm using a datagridview to display records. The functionality I need is when I press "Enter" key the focus should go to the next cell(column of same row). If it's the last column in the grid then the focus should go to the first column of the next row. I've already tried using

    SendKeys.Send("{Tab}")

但是焦点正在向对角线移动。请帮我解决这个问题。

in datagridview1_KeyDown and datagridview1_KeyPress event. But focus is moving diagonally down. Please help me to solve this.

推荐答案

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    e.SuppressKeyPress = true;
    int iColumn = dataGridView1.CurrentCell.ColumnIndex;
    int iRow = dataGridView1.CurrentCell.RowIndex;
    if (iColumn == dataGridView1.Columncount-1)
    {
        if (dataGridView1.RowCount > (iRow + 1))
        {
            dataGridView1.CurrentCell = dataGridView1[1, iRow + 1];
        }
        else
        {
            //focus next control
        }
    }
    else
        dataGridView1.CurrentCell = dataGridView1[iColumn + 1, iRow];
}

这篇关于如何将重点放在datagridview中的下一个单元格中输入按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆