DataGridView自定义DateTimePicker时间格式无效是什么原因
学着网上资料尝试着给DataGridView控件做了一个DateTimePicker类,但是无论如何设置都只显示日期,请帮助看看问题出在哪儿了:
Public Class CalendarColumn
Inherits DataGridViewColumn
Public Sub New()
MyBase.New(New CalendarCell())
End Sub
Public Overrides Property CellTemplate() As System.Windows.Forms.DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As System.Windows.Forms.DataGridViewCell)
If value IsNot Nothing AndAlso Not value.[GetType]().IsAssignableFrom(GetType(CalendarCell)) Then
Throw New InvalidCastException("Must be a CalendarCell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
Public Class CalendarCell
Inherits DataGridViewTextBoxCell
Public Sub New()
MyBase.New()
'use the "s" format.
Me.Style.Format = "s"
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 DTP = TryCast(DataGridView.EditingControl, DTP)
If Me.Value IsNot Nothing Then
ctl.Value = CType(Me.Value, DateTime) 'Date.Today
End If
End Sub
'定义DateTimePicker子类,限制日期显示格式
Class DTP
Inherits DateTimePicker
Public Sub New()
MyBase.new()
End Sub
Protected Overrides Sub InitLayout()
MyBase.InitLayout()
Me.Format = DateTimePickerFormat.Custom
Me.CustomFormat = "yyyy-MM-dd HH:mm:ss"
Me.ShowUpDown = False
End Sub
End Class
[解决办法]
解决了最好应该把方法留下吧!供以后参考啊!