vb.net 如何在为 datagridview 组合框单元格确认选择时提交编辑或结束编辑?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24701892/
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
How to commit edit or end edit on selection confirmed for datagridview combobox cell?
提问by Kat
Problem:I want to commit an edit for when the user has a datagridview combobox cell, i.e. it would be the regular combo box cell's "Selection Committed"
问题:当用户有一个 datagridview 组合框单元格时,我想提交一个编辑,即它将是常规组合框单元格的“选择提交”
What I've Tried:So based off a lot of stackflow questions (like Datagridview comboBox not selecting on click/edit) where they look at the event called such as selected index changed, the cell value changed, cell content click, and edit control showing. In these event handlers I have tried to commit the edit, but it instead calls this event afteranother cell has clicked, not once the selected item has been chosen in the cell. I've already tried making a "selected index changed" handler and definition, but since the combobox cell doesn't have that event, I get an error in vb.net
我尝试过的:因此基于许多堆栈流问题(例如Datagridview 组合框未选择单击/编辑),他们查看调用的事件,例如选定索引更改、单元格值更改、单元格内容单击和编辑控件显示。在这些事件处理程序中,我尝试提交编辑,但它会在单击另一个单元格后调用此事件,而不是在单元格中选择所选项目后。我已经尝试制作“选定的索引已更改”处理程序和定义,但是由于组合框单元格没有该事件,因此我在 vb.net 中收到错误
Here's a sample code snippet:
这是一个示例代码片段:
Private Sub editingComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
editCameraTable.CommitEdit(DataGridViewDataErrorContexts.Commit)
editCameraTable.EndEdit()
End Sub
This isn't called until another cell is clicked or otherwise. Any suggestions on how to flag this event for a datagridviewcomboboxcell?
直到单击另一个单元格或以其他方式才调用此方法。关于如何为 datagridviewcomboboxcell 标记此事件的任何建议?
回答by Nathan
Handel the CellEndEdit action. Yes it only fires after the person clicks elsewhere; but combine it with a keydown preview.. People naturally click return at the end of a text edit and you can use this to trigger the update. A Cell in a gridview can't determine when a person is done typing in it, so it needs the person to click out of it before the edit commit occurs. The only other way around this is perhaps using a timer that resets on each keydown event and updates if the typing goes idle.
处理 CellEndEdit 操作。是的,它只会在用户点击别处后才会触发;但将它与按键预览结合起来。人们自然会在文本编辑结束时单击返回,您可以使用它来触发更新。gridview 中的 Cell 无法确定一个人何时完成输入,因此它需要该人在编辑提交发生之前单击它。解决此问题的唯一另一种方法可能是使用一个计时器,该计时器在每个按键事件时重置并在输入空闲时更新。
Private Sub datagridview1_CellEndEdit(ByVal sender As Object, _
ByVal e As System.EventArgs) HAndels datagridview1.CellEndEdit
' If InLoad = True Then Exit Sub ' may want to suppress if you are programmatically changing this
datagridview1.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
' Do your saves...
End Sub
Private Sub datagridview1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) _
Handles datagridview1.KeyDown
If e.KeyCode = Keys.Return Then
datagridview1.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
End If
End Sub

