DataGridView 在 Enter/Return 上导航
我正在尝试使用 Enter 键移至 DataGridView 中的下一个(非只读)列。在正常情况下,我处理 KeyDown 事件并且一切正常。
但是,当我编辑字段时,Enter 键始终会完成编辑,然后移至下一个行。
奇怪的是,我在测试台上得到了以下一系列事件。 (括号中的数字是事件参数的(rowIndex,columnIndex))。
没有备用导航:
按 Enter 键(不编辑字段):
PreviewKeyDown key = Return
KeyDown key = Return
CellLeave (0, 0)
RowLeave (0, 0)
CellValidating (0, 0)
CellValidated (0, 0)
RowEnter (1, 0)
CurrentCellChanged
CellEnter (1, 0)
SelectionChanged
KeyUp key = Return
按 Enter 键(编辑字段时):
CellClick (0, 0)
CellBeginEdit (0, 0)
EditingControlShowing
(I press Enter now)
CellLeave (0, 0)
RowLeave (0, 0)
CellValidating (0, 0)
CellValidated (0, 0)
CellEndEdit (0, 0)
RowEnter (1, 0)
CurrentCellChanged
CellEnter (1, 0)
SelectionChanged
KeyUp key = Return
即,当我在编辑字段时按 Enter 键时,我永远不会收到 PreviewKeyDown 或 KeyDown 事件。
我尝试在事件处理程序中破解一些备用导航。出于测试目的,我总是编辑 (r0, c0),并通过将 CurrentCell 属性显式设置为 (r0, c1) 来“导航”。
当我在 CellEndEdit 中设置 CurrentCell 时,我最终得到 (r1, c1)。当我在 KeyUp 中设置 CurrentCell 时,我最终得到 (r0, c1),这很好,但前提是它明显选择了 (r1, c0)。我想我可以在导航过程中禁用控件的更新,但这个解决方案似乎不太优雅。
有什么建议吗?我错过了什么吗?
I'm trying to use the Enter key to move to the next (non-read-only) column in a DataGridView. Under normal circumstances, I handle the KeyDown event and everything works great.
However, when I'm editing a field, the Enter key always completes the edit and then moves to the next row.
Weirdly enough, I'm getting the following sequence of events in my test bed. (Numbers in parentheses are (rowIndex, columnIndex) of the event arg).
With no alternate navigation:
Press Enter (without editing the field):
PreviewKeyDown key = Return
KeyDown key = Return
CellLeave (0, 0)
RowLeave (0, 0)
CellValidating (0, 0)
CellValidated (0, 0)
RowEnter (1, 0)
CurrentCellChanged
CellEnter (1, 0)
SelectionChanged
KeyUp key = Return
Press Enter (while editing the field):
CellClick (0, 0)
CellBeginEdit (0, 0)
EditingControlShowing
(I press Enter now)
CellLeave (0, 0)
RowLeave (0, 0)
CellValidating (0, 0)
CellValidated (0, 0)
CellEndEdit (0, 0)
RowEnter (1, 0)
CurrentCellChanged
CellEnter (1, 0)
SelectionChanged
KeyUp key = Return
i.e. I never get a PreviewKeyDown or KeyDown event when I press Enter while editing a field.
I tried hacking some alternate navigation in the event handlers. For test purposes, I always edit (r0, c0), and "navigate" by setting the CurrentCell property explicity to (r0, c1).
When I set the CurrentCell in CellEndEdit, I end up at (r1, c1). When I set the CurrentCell in KeyUp, I end up at (r0, c1), which is good, but only after it visibly selects (r1, c0). I suppose I could disable updates on the control during navigation, but this solution seems inelegant.
Any advice? Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过
随机胡乱仔细分析,看起来关键是从DataGridView派生并重写DataGridView.ProcessDialogKey()和ProcessDataGridViewKey()After
random flailing aboutcareful analysis, it looks like the key is to derive from DataGridView and override DataGridView.ProcessDialogKey () and ProcessDataGridViewKey ()