C# 将击键发送到 DataGridViewCell
我的 DataGridView 中有一个 DateTimePicker 单元格。我希望能够进入编辑模式并在单击按钮时删除日历。我可以毫无困难地完成第一部分,但第二部分不起作用。如果我有一个独立的 DateTimePicker,则 SendKeys 调用确实会按预期工作。
//Select the cell and enter edit mode - works
myDGV.CurrentCell = myDGV[calColumn.Index, e.RowIndex];
myDGV.BeginEdit(true);
//Send an ALt-Down keystroke to drop the calendar - doesn't work
SendKeys.SendWait("%{DOWN}");
通过调试,我认为问题在于击键被发送到 DGV,而不是我要编辑的特定单元格。我认为的原因是我已将代码放入日志由网格 KeyPress 和 KeyDown 事件接收的按键中。它们记录我在网格周围的箭头和 SendKeys 发送的按键,但不记录我通过在其中键入来编辑单元格时的按键。
I have a DateTimePicker cell in my DataGridView. I'd like to be able to enter edit mode and drop the calendar when a button is clicked. I'm able to do the first part without difficulty but the second isn't working. If I have a standalone DateTimePicker the SendKeys call does work as expected.
//Select the cell and enter edit mode - works
myDGV.CurrentCell = myDGV[calColumn.Index, e.RowIndex];
myDGV.BeginEdit(true);
//Send an ALt-Down keystroke to drop the calendar - doesn't work
SendKeys.SendWait("%{DOWN}");
From debugging I believe that the problem is that the keystroke is being sent to the DGV and not the specific cell that I'm trying to edit. The reason I think is is that I've put code to log keys received by the grids KeyPress and KeyDown events. They log my arrowing around the grid and the keys sent by SendKeys, but not those from when I'm editing a cell by typing in it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅我在C# Winforms DataGridView时间列上的答案。我相信它会完美地满足您的需求。您还可以将其用于具有组合框的列。
Please see my answer on C# Winforms DataGridView Time Column. I believe it will fit your needs perfectly. You can also use it for a column that has a ComboBox.
我最近重新审视了这个问题,因为 0A0D 提供的实现并不总是能很好地与网格的键盘导航(箭头/选项卡)配合使用。有时可以绕过
DateTimePicker
并将文本输入到DataGridViewTextBoxCell
中。这导致我的验证逻辑崩溃了;在未能找到防止滑动发生的方法之后,我决定尝试让自定义列再次工作。事实证明修复非常简单。我创建了一个扩展的
DateTimePicker
,其中包含一种发送显示日历所需的击键的方法。然后,我修改了 MSDN DateTime 列示例 以具有
CalendarEditingControl< /code> 继承自
DateTimePickerEx
。然后,在托管
DataGridView
的表单中,我使用EditingControl
属性调用ShowCalendar()
方法。I recently revisited this issue because the implementation provided by 0A0D didn't always play nicely with keyboard navigation of the grid (arrows/tab). At times it was possible to bypass the
DateTimePicker
and enter text into theDataGridViewTextBoxCell
. This caused my validation logic to freak out; and after failing to find a way to prevent the slip around from happening I decided to try and get the custom column working again.The fix turned out to be very simple. I created an extended
DateTimePicker
with a method to send the keystroke needed to display the calendar.I then modified the MSDN DateTime column example to have
CalendarEditingControl
inherit fromDateTimePickerEx
.Then in the form hosting the
DataGridView
I used theEditingControl
property to call theShowCalendar()
method.