在单元格编辑模式不火的onkeydown在C#中的事件 [英] Cell in editmode doesn't fire OnKeyDown event in C#

查看:291
本文介绍了在单元格编辑模式不火的onkeydown在C#中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了这ovveride onKeyDown事件的DataGridView控件:

I've made own datagridview control which ovveride OnKeyDown event:

public partial class PMGrid : DataGridView
{
    protected override void OnKeyDown(KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    e.SuppressKeyPress = true; //suppress ENTER
                    //SendKeys.Send("{Tab}"); //Next column (or row)
                    base.OnKeyDown(e);
                }
                else if (e.KeyCode == Keys.Tab)
                {
                    base.OnKeyDown(e);
                    this.BeginEdit(false);
                }
                else
                {
                    base.OnKeyDown(e);
                }
            }
}

当我点击的DataGridView和按回车键就可以完美运行,因为排不改变和KeyUp事件。但是,当我按下Tab键,下一个单元格被选中,它更改为编辑模式。当我按下这个单元格中输入不开枪的KeyPress太KeyUp事件。
我尝试这样做,用户可以从一个细胞移动到下一个单元格,然后用户可以在该单元格写些东西,然后当用户按下Enter键将该值保存到数据库中。但是,当细胞是在编辑模式下,我不能检测到用户按下回车键。

When I click on datagridview and press Enter it works perfect because row isn't changed and KeyUp event is fired. But when I press Tab, next cell is selected and it is changed to EditMode. And when I press Enter in this cell KeyUp event isn't fired and KeyPress too. I try to do that user can move from one cell to next cell and then user can write something in this cell and then when user press Enter this value is saved to the database. But when cell is in EditMode I cannot detect that user press Enter.

感谢

推荐答案

您应该调用在的 EditingControlShowing 在DataGridView事件。像这样的东西应该工作:

You should call the KeyPress or KeyUp event in the EditingControlShowing event of the datagridview. Something like this should work:

private void dtgrd1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    var txtEdit = (TextBox)e.Control;
    txtEdit.KeyPress += EditKeyPress; //where EditKeyPress is your keypress event
}


private void EditKeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true; //suppress ENTER
                //SendKeys.Send("{Tab}"); //Next column (or row)
                base.OnKeyDown(e);
            }
            else if (e.KeyCode == Keys.Tab)
            {
                base.OnKeyDown(e);
                this.BeginEdit(false);
            }
            else
            {
                base.OnKeyDown(e);
            }

}

让我知道如果您有任何疑惑在实施该代码。

Let me know if you have any doubts while implementing this code.

修改

要避免去到下一个排进入,请检查该解决的问题:的如何防止将下一行编辑DataGridViewTextBoxColumn并按EnterKey?

To avoid going to the next row on enter, please check this resolved question: How to prevent going to next row after editing a DataGridViewTextBoxColumn and pressing EnterKey?

这篇关于在单元格编辑模式不火的onkeydown在C#中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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