我有一个奇怪的问题,在VB.NET中重载DataGridView的单元格以使用DateTimePicker(允许用户同时选择日期和时间)。我拿了Microsoft implementation of a Calendar picker并将其修改为日历和时间选择器。当我输入新日期并按ENTER键时,DataGridView单元格中显示的日期是旧日期。
仅当在该键上再次按ENTER时,或者单击该单元格并单击该键显示新值。
如果单击单元格,输入新日期并单击另一个单元格,此单元格会显示正确的日期时间值。然后,显示的日期与我输入的日期相匹配。它使用我遇到问题的ENTER键。
我修改了Microsoft重载代码,如下所示:
Public Class TimeColumn
Inherits DataGridViewColumn
Public Sub New()
MyBase.New(New TimeCell())
End Sub
Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As DataGridViewCell)
' Ensure that the cell used for the template is a TimeCell.
If (value IsNot Nothing) AndAlso _
Not value.GetType().IsAssignableFrom(GetType(TimeCell)) _
Then
Throw New InvalidCastException("Must be a TimeCell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
Public Class TimeCell
Inherits DataGridViewTextBoxCell
Public Sub New()
' Use the short date format.
Me.Style.Format = "MM/dd/yy h:mm tt"
'm_isTime = True
End Sub
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
ByVal initialFormattedValue As Object, _
ByVal dataGridViewCellStyle As DataGridViewCellStyle)
' Set the value of the editing control to the current cell value.
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
dataGridViewCellStyle)
Dim ctl As New TimeEditingControl
ctl = CType(DataGridView.EditingControl, TimeEditingControl)
ctl.CustomFormat = "MM/dd/yyyy h:mm tt"
Me.Value = ctl.Value
End Sub
Public Overrides ReadOnly Property EditType() As Type
Get
' Return the type of the editing contol that TimeCell uses.
Return GetType(TimeEditingControl)
End Get
End Property
Public Overrides ReadOnly Property ValueType() As Type
Get
' Return the type of the value that TimeCell contains.
Return GetType(DateTime)
End Get
End Property
Public Overrides ReadOnly Property DefaultNewRowValue() As Object
Get
' Use the current date and time as the default value.
Return DateTime.Now
End Get
End Property
End Class
Class TimeEditingControl
Inherits DateTimePicker
Implements IDataGridViewEditingControl
Private dataGridViewControl As DataGridView
Private valueIsChanged As Boolean = False
Private rowIndexNum As Integer
Public Sub New()
Me.Format = DateTimePickerFormat.Custom
End Sub
Public Property EditingControlFormattedValue() As Object _
Implements IDataGridViewEditingControl.EditingControlFormattedValue
Get
Return Me.Value.ToShortDateString()
End Get
Set(ByVal value As Object)
If TypeOf value Is String Then
Me.Value = DateTime.Parse(CStr(value))
End If
End Set
End Property
Public Function GetEditingControlFormattedValue(ByVal context _
As DataGridViewDataErrorContexts) As Object _
Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
Dim result, tempDateTime As DateTime
tempDateTime = Me.Value
Return tempDateTime.ToString
End Function
Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As _
DataGridViewCellStyle) _
Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
Me.Font = dataGridViewCellStyle.Font
Me.CalendarForeColor = dataGridViewCellStyle.ForeColor
Me.CalendarMonthBackground = dataGridViewCellStyle.BackColor
End Sub
Public Property EditingControlRowIndex() As Integer _
Implements IDataGridViewEditingControl.EditingControlRowIndex
Get
Return rowIndexNum
End Get
Set(ByVal value As Integer)
rowIndexNum = value
End Set
End Property
Public Function EditingControlWantsInputKey(ByVal key As Keys, _
ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
Implements IDataGridViewEditingControl.EditingControlWantsInputKey
Dim lDateTime As DateTime
' Let the DateTimePicker handle the keys listed.
Select key And Keys.KeyCode
Case Keys.Enter
Console.WriteLine(DateTime.Now.ToString & "." & DateTime.Now.Millisecond.ToString & ": EditingControlWantsInputKey--Detected [ENTER] key.")
End Select
valueIsChanged = True
Console.WriteLine("EditingControlWantsInputKey (" & Me.Value & ")")
Dim lDataGridView1 As DataGridView
lDataGridView1 = Me.dataGridViewControl
lDataGridView1.BeginEdit(True)
lDataGridView1.BeginEdit(False)
Return True
End Function
Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
' No preparation needs to be done.
End Sub
Public ReadOnly Property RepositionEditingControlOnValueChange() _
As Boolean Implements _
IDataGridViewEditingControl.RepositionEditingControlOnValueChange
Get
Return False
End Get
End Property
Public Property EditingControlDataGridView() As DataGridView _
Implements IDataGridViewEditingControl.EditingControlDataGridView
Get
Return dataGridViewControl
End Get
Set(ByVal value As DataGridView)
dataGridViewControl = value
End Set
End Property
Public Property EditingControlValueChanged() As Boolean _
Implements IDataGridViewEditingControl.EditingControlValueChanged
Get
Return valueIsChanged
End Get
Set(ByVal value As Boolean)
valueIsChanged = value
End Set
End Property
Public ReadOnly Property EditingControlCursor() As Cursor _
Implements IDataGridViewEditingControl.EditingPanelCursor
Get
Return MyBase.Cursor
End Get
End Property
Protected Overrides Sub OnValueChanged(ByVal eventargs As EventArgs)
' Notify the DataGridView that the contents of the cell have changed.
valueIsChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
MyBase.OnValueChanged(eventargs)
End Sub
End Class
到目前为止我的发现:
有什么好奇的是公共属性EditingControlValueChanged()As Boolean实现IDataGridViewEditingControl.EditingControlValueChanged 根本不检测ENTER键!您可以看到我为该事件输入了一些调试代码,以查看它是否检测到按下了ENTER键。
事件公共属性EditingControlValueChanged()As Boolean实现IDataGridViewEditingControl.EditingControlValueChanged 未检测到值更改(布尔值isIsChanged设置为false),直到第二次按下ENTER键为止,此时它被设置为true。
我还尝试更新DataGridView的CellEndEdit事件中的单元格值,但这没有用。
我已经更新了我的Visual Studio Express 2013,以防它是Micosoft的错误。
如何获取显示的值以匹配按ENTER键时输入的内容?感谢。
答案 0 :(得分:0)
我最初的评论肯定是错的。在重现你的问题之后,我注意到一些有趣/令人沮丧的事情:在下一行的EditingControlWantsInputKey
方法中放置一个断点,允许更新按预期发生(调试问题的修复) :
Select key And Keys.KeyCode
这太烦人了,在我的沮丧中,我连续疯狂地测试了一些月份:1,2,3,...,12
那时我注意到:10-12个月正确更新了。实际上,输入01正确更新。我随后的研究引导我进入comments section of this article,其中用户Dean Wiles在他的评论中标题为使用Tab键输入的更改或输入提供了this Microsoft support source,表明:
[DateTimePicker]的ValueChanged事件仅在之后引发 您键入以下任何一项:
- 一年的所有数字。
- 一天的所有数字。
- 一个月的所有数字。
这可以通过将以下vb.net版本的代码添加到TimeEditingControl
类来解决:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
Select Case keyData And Keys.KeyCode
Case Keys.Enter, Keys.Tab
Me.dataGridViewControl.Focus()
Exit Select
End Select
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
答案 1 :(得分:0)
C#转换
%inc