Public Class DataGridViewDatetimepickerColumn
Inherits System.Windows.Forms.DataGridViewColumn
Public Sub New()
MyBase.New(New DataGridViewDatetimepickerCell())
End Sub
Private _PickerFormat As System.Windows.Forms.DateTimePickerFormat = System.Windows.Forms.DateTimePickerFormat.[Long]
' [System.ComponentModel.DefaultValue(System.Windows.Forms.DateTimePickerFormat.Long)]
<System.ComponentModel.Browsable(True)> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
<System.ComponentModel.Category("Behavior")> _
<System.ComponentModel.Description("Open Dialog is dispalyed and on success the contents of the Cell is replaced with the new path.")> _
Public Property PickerFormat() As System.Windows.Forms.DateTimePickerFormat
Get
Return _PickerFormat
End Get
Set(value As System.Windows.Forms.DateTimePickerFormat)
_PickerFormat = value
End Set
End Property
Public Overrides Function Clone() As Object
Dim clm = TryCast(MyBase.Clone(), DataGridViewDatetimepickerColumn)
Dim xxx As DataGridViewDatetimepickerColumn = TryCast(MyBase.Clone(), DataGridViewDatetimepickerColumn)
If clm IsNot Nothing Then
clm.PickerFormat = _PickerFormat
End If
Return clm
End Function
Public Overrides Property CellTemplate() As System.Windows.Forms.DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(value As System.Windows.Forms.DataGridViewCell)
If (value IsNot Nothing) AndAlso Not value.[GetType]().IsAssignableFrom(GetType(DataGridViewDatetimepickerCell)) Then
Throw New InvalidCastException("Must be a DataGridViewDatetimepickerCell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
Public Class DataGridViewDatetimepickerCell
Inherits System.Windows.Forms.DataGridViewTextBoxCell
Public Sub New()
End Sub
Public Overrides Sub InitializeEditingControl(rowIndex As Integer, initialFormattedValue As Object, dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle)
' Set the value of the editing control to the current cell value.
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
Dim ctl As CalendarEditingControl = DirectCast(DataGridView.EditingControl, CalendarEditingControl)
If (Not Object.ReferenceEquals(Me.Value, DBNull.Value)) Then
If (Me.Value IsNot Nothing) Then
ctl.Value = DirectCast(Me.Value, DateTime)
End If
End If
Dim fileColumn As DataGridViewDatetimepickerColumn = DirectCast(Me.DataGridView.Columns(ColumnIndex), DataGridViewDatetimepickerColumn)
ctl.Format = fileColumn.PickerFormat
End Sub
Public Overrides ReadOnly Property EditType() As Type
' Return the type of the editing contol that DataGridViewDatetimepickerCell uses.
Get
Return GetType(CalendarEditingControl)
End Get
End Property
Public Overrides ReadOnly Property ValueType() As Type
' Return the type of the value that DataGridViewDatetimepickerCell contains.
Get
Return GetType(DateTime)
End Get
End Property
End Class
Public Class CalendarEditingControl
Inherits DateTimePicker
Implements IDataGridViewEditingControl
Private dataGridViewControl As DataGridView
Private valueIsChanged As Boolean = False
Private rowIndexNum As Integer
Public Sub New()
Me.Format = DateTimePickerFormat.Short
End Sub
Public Property EditingControlFormattedValue() As Object _
Implements IDataGridViewEditingControl.EditingControlFormattedValue
Get
Return Me.Value.ToShortDateString()
End Get
Set(ByVal value As Object)
Try
' This will throw an exception of the string is
' null, empty, or not in the format of a date.
Me.Value = DateTime.Parse(CStr(value))
Catch
' In the case of an exception, just use the default
' value so we're not left with a null value.
Me.Value = DateTime.Now
End Try
End Set
End Property
Public Function GetEditingControlFormattedValue(ByVal context _
As DataGridViewDataErrorContexts) As Object _
Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
Return Me.Value.ToShortDateString()
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
' Let the DateTimePicker handle the keys listed.
Select Case key And Keys.KeyCode
Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _
Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp
Return True
Case Else
Return Not dataGridViewWantsInputKey
End Select
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