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

查看:25
本文介绍了如何在 Enter 按键事件上将焦点移到 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}")

在 datagridview1_KeyDown 和 datagridview1_KeyPress 事件中.但焦点正在斜向下移动.请帮我解决这个问题.

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

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

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