如何在DataGridView中使用Enter键作为Tab键 [英] How to use Enter key as Tab key in DataGridView

查看:138
本文介绍了如何在DataGridView中使用Enter键作为Tab键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个5列的DataGridView.如果在第一列中按Enter键,则焦点将移至下一行.我想在按Enter键时将焦点移到下一个.

I have a DataGridView with 5 columns. If press Enter key in the first column, focus moves to next row. I want to move the focus to the next column when I press Enter key.

 private void dgvComp_CellEnter(object sender, DataGridViewCellEventArgs e)
 {
    if (dgvComp.CurrentRow.Cells[e.ColumnIndex].ReadOnly)
    {
       SendKeys.Send("{tab}");

    }   
 }

在上面的代码中,我将第2、3和4列作为只读列.如果按Tab键,焦点应直接移至第5列.

In the above code I have columns 2,3 and 4 as read only columns. If I press Tab, focus should directly go to 5th column.

我该怎么做?

推荐答案

您能否尝试此解决方案

using System.Diagnostics;
class MyDataGridView : DataGridView
{

    protected override bool ProcessDialogKey(Keys keyData)
    {
        if (keyData == Keys.Enter) {
            base.ProcessTabKey(Keys.Tab);
            return true;
        }
        return base.ProcessDialogKey(keyData);
    }

    protected override bool ProcessDataGridViewKey(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter) {
            base.ProcessTabKey(Keys.Tab);
            return true;
        }
        return base.ProcessDataGridViewKey(e);
    }

}

这篇关于如何在DataGridView中使用Enter键作为Tab键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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