如何在 DataGridView 中显示 DateTimePicker?
有什么方法可以将 DateTimePicker 控件放入 DataGridView 中吗?
我检查了所有可能的属性,但它提供了复选框、组合框等选项,但没有提供 DateTimePicker。
Is there any way to put a DateTimePicker control in the DataGridView?
I checked all the possible properties but it give option of checkbox, combo box etc, but not the DateTimePicker.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您没有错过任何内置选项,但可以对
DataGridViewColumn
和DataGridViewCell
类进行子类化以托管您选择的任何控件。MSDN 上的这篇文章更详细地解释了该过程,甚至包括一些示例代码:
如何:在 Windows 窗体 DataGridView 单元格中托管控件
您还可以在代码项目上找到完整的示例: 通用 DataGridView V2.0
You haven't missed any built-in option, but it is possible to subclass both the
DataGridViewColumn
andDataGridViewCell
classes to host any control of your choosing.This article on MSDN explains the process in more detail, and even includes some sample code:
How to: Host Controls in Windows Forms DataGridView Cells
You can also find a complete sample on Code Project: Generic DataGridView V2.0
一种策略是:
DateTimePicker
这是一种使用布局策略处理网格事件的方法:
这是添加了 DataGridView 的表单的基本设置
One strategy would be:
DateTimePicker
on top of the selected cell when it receives focusHere's a way to handle grid events with the laid out strategy:
And Here's a basic setup for a form that has a DataGridView added to it
也许这不合适,但简单的技巧和相同的结果......更少的代码......我只是在玩,虽然在盒子外面,只需设置
我隐藏我的直到他们单击单元格,或者你可以显示
首先我声明:
当您单击单元格时,运行此代码...
然后在
Super Basic 下方,我将它直接放在框中,看起来不合适
或超级简单模式......我就像隐藏我的直到列单击
你真的只需要一行......数据将在网格中,只是少了很多代码......
Maybe this isn't proper, but easy trick and same result........ lot less code......I was just playing around and though outside the box, just set
I hide mine until they click cell, or you can show
First I declared :
when you click in cell, run this code...
Then below
Super Basic , and i have it sitting directly in the box, doesn't look out of place
Or super easy mode.......I just like to hide mine till column click
You really just need that one line.....data will be in the grid, just a lot less code.....
好的...使用一些 @rajat 和 @Aaron 示例,我制作了一个在 DateTimePicker 单元格上弹出的示例。谢谢大家。
Ok... Using some of @rajat and @Aaron examples, i made one that pops up on the DateTimePicker cell. Thanks everyone.
要解决在 DataGridView 中使用 DateTimePicker 时的一些输入问题,您需要将以下内容添加到上面引用的 Microsoft 示例中。我花了相当长的时间才找到 valuechanged 事件未按预期触发的问题。修复来自这里(stackoverflow)并在下面翻译成 C#。在此处添加此信息似乎很合适,因为我在 DataGridView 和 DateTimePicker 上搜索时不断找到此论坛帖子。
To solve some entry issues when using the DateTimePicker in a DataGridView you will want to add the following to the Microsoft Sample referenced above. It took quite a while to search out the problems with the valuechanged event not firing as expected. The fix came from here (stackoverflow) and translated to C# below. It seemed appropriate to add this information here as I keep finding this forum post when searching on DataGridView and DateTimePicker.
使用 RowsAdded 事件,如下所示 -
Use RowsAdded Event shown as below-
我认为 DateTimePicker 上的 CloseUp 事件更合适,因为更改的值会在任何更改时触发,而 CloseUp 仅在选择整个日期时触发
I think the CloseUp event on DateTimePicker is more appropriate because the changed value is triggering on any change while CloseUp only triggers when the entire date is selected