C# Winforms DataGridView 时间列
如何在 DataGridView 中显示时间选择器列?
我不需要选择日期。我只需要时间被选中。
How to show a Time-picker column in DataGridView?
I don't need to pick Date. I only need time to be selected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
寻找同样的东西我终于找到了一篇 MSDN 文章它展示了如何创建自定义 CalendarColumn,因此我使用了该代码示例并对其进行了修改以创建 TimeColumn。它工作得很好 - 非常干净并且基于 Microsoft 代码示例。不是黑客攻击,可以有效、可靠地进行数据绑定。
要实现,只需将这些类添加到您的项目中,然后在 DataGridView 的 ColumnType 字段中选择 TimeColumn。
Looking for this same thing I finally found an MSDN article that shows how to make a custom CalendarColumn so I used that code sample and modified it to create a TimeColumn. It works great - very clean and based on Microsoft Code sample. Not a hack and can be databound efficiently and dependably.
To implement simply add these classes to your project and then select TimeColumn in the ColumnType field of the DataGridView.
实际上,有比创建自定义 DataGridView 列更好的方法。我当前为应用程序所做的事情是,当输入 DataGridView 的 TIMESTAMP 列时,我将 DateTimePicker 控件直接放置在单元格上。当用户单击单元格外(从而确认其选择)时,DateTimePicker Visible 将设置为 False,并且 DateTimePicker 的值将放入单元格中。默认情况下,DateTimePicker 控件的 Visibility 设置为 False,直到我需要它为止。我还将其用于常规单元格上的组合框控件,其中用户无法输入自定义值并且必须使用设置屏幕中的项目列表。这种技术非常适合伪造。我没有现成的代码,但恕我直言,它的代码更少并且更容易维护。
上述和以下技术取自在 Win Forms 2.0 中的 DataGridView 控件中伪造替代控件
编辑:这是代码 -
}
Actually, there is a better way than creating a custom DataGridView column. What I currently do for my application is when the TIMESTAMP column of my DataGridView is entered, I position a DateTimePicker control directly over the cell. When the user has clicked out of the cell (thus confirming his selection), the DateTimePicker Visible is set to False and the DateTimePicker's value is put into the cell. By default, the DateTimePicker control Visibility is set to False until I need it. I also use this for ComboBox controls on regular cells where the user cannot enter a custom value and has to use the list of items from the Setup Screen. This technique is great for faking it. I don't have the code readily available, but it is less code and easier to maintain IMHO.
The above and following technique was taken from Faking alternative controls within a DataGridView control in Win Forms 2.0
Edit: Here is the code -
}
AFAIK,有一种直接的方法可以做到这一点。我认为唯一的方法是创建自定义 DataGridview 列。检查此链接以了解创建自定义 datagridview 列
AFAIK, there is a direct way to do this. I think the only way is by creating a Custom DataGridview column. Check this link for creating a custom datagridview column
很抱歉回复旧线程,但这是我唯一能找到有人像我一样改编了 MS 代码的地方。作为对 RThomas 的回应,我已经这样做了,但我发现在绑定到数据表时使用 TimeColumn 存在问题。我将它用于员工时间表输入,因此日期元素不可见,只有时间。当我输入数据时,它工作正常,但当我尝试编辑数据时,出现了问题。
OnValueChanged 事件中的单元格值是正确的(使用“调试”我可以看到 Me.Value 的值),但在 DataGridView.CellEndEditing 或 CellValidated 事件中,该值显示当前日期和编辑的时间值。奇怪的是,如果用户移动到另一个单元格,然后返回到 TimeCell,然后再次移动到另一个单元格,则值是正确的,但在初始编辑后,日期是错误的。
我已经查看了所有不同的事件,但它似乎是在 DataGridView 中编辑的值和提交到绑定到 DataGridView 的数据表之间的某种值。
Sorry for replying to an old thread, but this is the only place I could find where someone has adapted the MS code as I have. In response to RThomas, I have done this but I am finding problems with using the TimeColumn when bound to a datatable. I am using it for employees timesheet entry so the date element is not visible, just the time. When I enter the data it works fine, but when I try editing the data something goes wrong.
The cell value is correct in the OnValueChanged event (using Debug I can see the value of Me.Value) but in the DataGridView.CellEndEditing or CellValidated events the value shows teh current date and the edited time value. Strangely, if the user moves to another cell, then back to the TimeCell, then to another cell again the value is correct, but after the initial edit the date is wrong.
I've looked at all different events but it seems to be something between the value being edited in the DataGridView and committed to the Datatable bound to the DataGridView.