C# 我可以让 DataGridView.EndEdit 触发 CellValidating 事件吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/422865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I make DataGridView.EndEdit trigger the CellValidating event?
提问by Don Kirkby
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.
我在 WinForms 应用程序中使用 DataGridView。我的主要目标是使 Enter 键不会移动到网格中的下一行。我仍然希望输入键来验证和结束编辑模式。
I found this FAQ entryand subclassed DataGridView to override ProcessDialogKey(). If the key pressed is Enter, I call EndEdit(), otherwise I call base.ProcessDialogKey().
我发现这个 FAQ 条目和子类 DataGridView 来覆盖 ProcessDialogKey()。如果按下的键是 Enter,则调用 EndEdit(),否则调用 base.ProcessDialogKey()。
It works great, except the CellValidating event isn't fired.
它工作得很好,除了 CellValidating 事件没有被触发。
Currently, I'm just manually calling my validation logic before I call EndEdit, but it seems like I'm missing something.
目前,我只是在调用 EndEdit 之前手动调用我的验证逻辑,但似乎我遗漏了一些东西。
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.
我想我可以调用 OnCellValidating,但是我担心我会错过其他一些事件。我真正想要的是 EndEdit() 的某种风格,它的行为就像在添加禁用的网格的最后一行按下 Enter 一样。
采纳答案by JJO
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.
在您更改 CurrentCell 之前,不会调用 CellValidating。所以我纠结于此的方法是更改 CurrentCell,然后切换回当前的。
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Enter)
{
DataGridViewCell currentCell = CurrentCell;
EndEdit();
CurrentCell = null;
CurrentCell = currentCell;
return true;
}
return base.ProcessDialogKey(keyData);
}
回答by Malfist
No, but you can manually fire the CellValidating event. Just create the proper parameters. All events are is a class using the Observer Pattern, they're no different than any other method. If that doesn't work, you can create a KeyPress event on the cell and emulate pressing Enter on the cell, but that may mess with the users UI, just put the carat back where it was.
不,但您可以手动触发 CellValidating 事件。只需创建适当的参数。所有事件都是使用观察者模式的类,它们与任何其他方法没有什么不同。如果这不起作用,您可以在单元格上创建一个 KeyPress 事件并模拟在单元格上按下 Enter 键,但这可能会干扰用户 UI,只需将克拉放回原来的位置即可。
回答by user309091
thanks for the solution. my version is a slight different from yours, because when i move to the other cell, and my code returns e.cancel=false in the cell validating event, an error will be generated, says that: "operation did not succeed, because the program cannot commit or quit a cell value change". so i put try catch to overcome this problem.
感谢您的解决方案。我的版本和你的略有不同,因为当我移动到另一个单元格时,我的代码在单元格验证事件中返回 e.cancel=false,会产生一个错误,说:“操作没有成功,因为程序无法提交或退出单元格值更改”。所以我把 try catch 来克服这个问题。
this is my code:
这是我的代码:
Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
Dim key As Keys = (keyData And Keys.KeyCode)
If key = Keys.Enter Then
If MyBase.CurrentCell.ColumnIndex = 1 Then
Dim iRow As Integer = MyBase.CurrentCell.RowIndex
MyBase.EndEdit()
Try
MyBase.CurrentCell = Nothing
MyBase.CurrentCell = MyBase.Rows(iRow).Cells(1)
frmFilter.cmdOk_Click(Me, New EventArgs)
Catch ex As Exception
End Try
Return True
End If
End If
Return MyBase.ProcessDialogKey(keyData)
End Function
回答by Michael Buen
if your DataGridView's DataSource is BindingSouce, do this (put this in your Key processing events):
如果您的 DataGridView 的数据源是 BindingSouce,请执行此操作(将其放在您的关键处理事件中):
bds.EndEdit();
if your DataGridView's DataSource is DataTable:
如果您的 DataGridView 的数据源是 DataTable:
this.BindingContext[dgv.DataSource].EndCurrentEdit();
回答by PUG
JJO's code will crash if cell is in edit mode. Below avoids validation exception:
如果单元格处于编辑模式,JJO 的代码将崩溃。下面避免了验证异常:
DataGridViewCell currentCell = AttachedGrid.CurrentCell;
try
{
AttachedGrid.EndEdit();
AttachedGrid.CurrentCell = null;
AttachedGrid.CurrentCell = currentCell;
}
catch
{
AttachedGrid.CurrentCell = currentCell;
AttachedGrid.CurrentCell.Selected = true;
}
Source: Kennet Harris's answer here
资料来源:Kennet Harris 的回答在这里