本文介绍了我可以让 DataGridView.EndEdit 触发 CellValidating 事件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我在我的 WinForms 应用程序中使用 DataGridView.我的主要目标是使 Enter 键不会移动到网格中的下一行.我仍然需要回车键来验证和结束编辑模式.

I'm using a DataGridView in my WinForms application. My main objective is to make the Enter key not move to the next row in the grid. I still want the enter key to validate and end edit mode.

我发现 this FAQ entry 和子类化的 DataGridView 以覆盖 ProcessDialogKey().如果按下的键是 Enter,则调用 EndEdit(),否则调用 base.ProcessDialogKey().

I found this FAQ entry and subclassed DataGridView to override ProcessDialogKey(). If the key pressed is Enter, I call EndEdit(), otherwise I call base.ProcessDialogKey().

效果很好,只是没有触发 CellValidating 事件.

It works great, except the CellValidating event isn't fired.

目前,我只是在调用 EndEdit 之前手动调用我的验证逻辑,但似乎我遗漏了一些东西.

Currently, I'm just manually calling my validation logic before I call EndEdit, but it seems like I'm missing something.

我想我可以调用 OnCellValidating,但是我担心我会错过其他一些事件.我真正想要的是 EndEdit() 的某种风格,它的行为就像在网格的最后一行按下 Enter 并禁用添加一样.

I guess I could call OnCellValidating, but then I'd be worried I'm missing some other event. What I really want is some flavour of EndEdit() that behaves just like pressing enter on the last row of a grid with adding disabled.

推荐答案

在您更改 CurrentCell 之前,不会调用 CellValidating.所以我纠结于此的方法是更改​​ CurrentCell,然后切换回当前的.

CellValidating doesn't get called until you change the CurrentCell. So the way I kludged around this was to change the CurrentCell, then switch back to the current one.

    protected override bool ProcessDialogKey(Keys keyData)
    {
        if (keyData == Keys.Enter)
        {
            DataGridViewCell currentCell = CurrentCell;
            EndEdit();
            CurrentCell = null;
            CurrentCell = currentCell;
            return true;
        }
        return base.ProcessDialogKey(keyData);
    }

这篇关于我可以让 DataGridView.EndEdit 触发 CellValidating 事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:34