简体   繁体   English

在DataGridView控件中吞没了Keydown事件/未对其进行操作

[英]Keydown event being swallowed/not actioned in DataGridView control

I have a problem with some code I developed based on the article DataGridView keydown event not working in C# . 我根据文章DataGridView keydown事件开发的某些代码有问题,但在C#中不起作用

I wanted to allow the user to add a row to a Dataviewgrid control, but found that by enabling AllowUserToAddRows this caused an additional row to be shown as soon as the first character was typed into the new cell in the new row. 我想允许用户向Dataviewgrid控件添加一行,但是发现通过启用AllowUserToAddRows这导致在第一个字符键入新行的新单元格后立即显示另一行。 This would have been confusing to my poor user, so to prevent this, I used the code from above article to immediately disable AllowUserToAddRows at every keystroke (although would have preferred to only do it after the first char was typed). 这会使我的可怜的用户感到困惑,因此,为了避免这种情况,我使用了上一篇文章中的代码,在每次击键时立即禁用AllowUserToAddRows (尽管本来希望只在键入第一个字符之后才这样做)。 However, this seems to swallow the 1st char typed, i,.e. 但是,这似乎吞没了第一个字符,即。 it is not passed onto the base class for processing. 它不会传递给基类进行处理。 Here's the full code: 这是完整的代码:

public sealed class XDataGridView : DataGridView
{
    private bool _singleUpdateOnly = true;

    public bool SingleUpdateOnly
    {
        get { return _singleUpdateOnly; }
        set { _singleUpdateOnly = value; }
    }

    [Description("Disallows user adding rows as soon as a key is struck to ensure no blank row at bottom of grid")]
    protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
    {
        if (SingleUpdateOnly)
        {
            AllowUserToAddRows = false;
        }

        return base.ProcessCmdKey(ref msg, keyData);
    }
}

Why is the 1st char I type swallowed? 为什么我输入的第一个字符被吞下? How can I prevent this from happening? 如何防止这种情况发生? Is there a better way than what I have coded? 有没有比我编写的代码更好的方法?

I've had very little response to this question on this forum and others. 在这个论坛和其他论坛上,我对这个问题的回应很少。 It must be a very difficult problem, or I'm simply expecting too much from the DataGridView control. 这一定是一个非常棘手的问题,否则我只是对DataGridView控件期望过高。 After many hours of trial and error, I have settled on a solution which does not require subclassing. 经过数小时的反复试验,我确定了不需要子类化的解决方案。 The solution is not exactly what I wanted, ie to prevent another another row from being appended to the bottom of the grid once the user begins typing into the newly added row, but it comes close. 解决方案不完全是我想要的,即,一旦用户开始在新添加的行中键入内容,就可以防止将另一行附加到网格底部,但这很接近。 I use the CellLeave event to turn off AllowUserToAddRows . 我使用CellLeave事件来关闭AllowUserToAddRows When the user presses Tab or Enter to enter the data into the 1st cell, this effectively removes the newly added row from the grid leaving the user in the next cell of the newly added (last) row of the grid (without an empty row underneath). 当用户按Tab或Enter键将数据输入到第一个单元格时,这将有效地从网格中删除新添加的行,而将用户保留在网格中新添加的(最后)行的下一个单元格中(下面没有空行) )。 Not elegant, but at least mostly functional. 不优雅,但至少大部分是实用的。 Here's the code: 这是代码:

private void uxContactEmailAddressesGrd_CellLeave(object sender, DataGridViewCellEventArgs e)
{
    uxContactEmailAddressesGrd.AllowUserToAddRows = false;
}

Maybe one day someone else will come across this problem and find a solution more elegant than mine. 也许有一天,其他人会遇到这个问题,找到比我更优雅的解决方案。 That'd be great. 那简直太好了。 Make sure you post your solution to this site for others to use. 确保将解决方案发布到此网站上,以供其他人使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM