要求在datagrid视图中输入keypress事件 [英] asking enter keypress event in datagrid view

查看:74
本文介绍了要求在datagrid视图中输入keypress事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我按下Enter键时,datagrid转到下一行,然后更改我想要的当前单元格.
但是我想在按Enter键时需要它,而无需直接进入下一行即可直接获取我想要的当前单元格.
我想知道如何忽略输入的keypress事件.

when i pressed enter key , datagrid get to next row and then change current cell i want.
but i want to need that when i pressed enter key ,want to get directly current cell that i want without going next row .
i want to know how i can ingnore enter keypress event .

推荐答案

从理论上讲,您可以通过定义KeyPress EventHandler,然后设置将其KeyPressEventArgs EventHandler的"Handled"属性"e"更改为"true".

但是,实际上,这是行不通的! ...与其他控件一样,即使您定义了以下内容,当前行也将向下移动:
Theoretically you can ignore a key-press event in a DataGridView by defining a KeyPress EventHandler, and then setting the ''Handled property of its KeyPressEventArgs EventHandler, "e," to ''true.

But, in practice, that does not work ! ... as it does with other Controls, and the current row is going to move down even if you define this:
char EnterKey = (char)Keys.Enter;

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == EnterKey)
    {
        // here''s where you do whatever it is you want to do
        e.Handled = true; // does not work as expected
    }
}

相反,您将做了这样的变通方法:

Instead you are going to have do some work-around like this:

char EnterKey = (char)Keys.Enter;
DataGridViewCell currentCell;

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == EnterKey)
    {
        // here''s where you do whatever it is you want to do
        e.Handled = true; // does not work as expected
        dataGridView1.CurrentCell = currentCell;
    }
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    currentCell = dataGridView1.CurrentCell;
}

但是,在您的问题中:按Enter键时您真正想要发生的事情对我来说还不清楚.

为什么当按下Enter键时,为什么希望所选单元格在同一行上保持不变?

应该有很好的理由,有特殊的理由,要超越标准控件的预期默认行为.

But, in your question: what you actually want to happen when you press Enter is not clear to me.

Why do you want the selected cell to remain the same, on the same row, when the Enter key is pressed ?

There should be very good reasons, special reasons, for over-riding the expected default behaviors of standard controls.


这篇关于要求在datagrid视图中输入keypress事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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