Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Imports System.Windows.Forms

Public Class CalendarColumn
	Inherits DataGridViewColumn
	Public Sub New()
		MyBase.New(New CalendarCell())
	End Sub

	Public Overrides Property CellTemplate() As DataGridViewCell
		Get
			Return MyBase.CellTemplate
		End Get
		Set
			' Ensure that the cell used for the template is a CalendarCell.
			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 short date format.
		Me.Style.Format = "d"
	End Sub

	Public Overrides Sub InitializeEditingControl(rowIndex As Integer, initialFormattedValue As Object, dataGridViewCellStyle As DataGridViewCellStyle)
		' Set the value of the editing control to the current cell value.
		MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
		Dim ctl As CalendarEditingControl = TryCast(DataGridView.EditingControl, CalendarEditingControl)
		ctl.Value = CType(Me.Value, DateTime)
	End Sub

	Public Overrides ReadOnly Property EditType() As Type
		Get
			' Return the type of the editing contol that CalendarCell uses.
			Return GetType(CalendarEditingControl)
		End Get
	End Property

	Public Overrides ReadOnly Property ValueType() As Type
		Get
			' Return the type of the value that CalendarCell 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 CalendarEditingControl
	Inherits DateTimePicker
	Implements IDataGridViewEditingControl
	Private dataGridView As DataGridView
	Private valueChanged As Boolean = False
	Private rowIndex As Integer

	Public Sub New()
		Me.Format = DateTimePickerFormat.[Short]
	End Sub

	' Implements the IDataGridViewEditingControl.EditingControlFormattedValue
	' property.
	Public Property EditingControlFormattedValue() As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue
		Get
			Return Me.Value.ToShortDateString()
		End Get
		Set
			If TypeOf value Is [String] Then
				Me.Value = DateTime.Parse(DirectCast(value, [String]))
			End If
		End Set
	End Property

	' Implements the
	' IDataGridViewEditingControl.GetEditingControlFormattedValue method.
	Public Function GetEditingControlFormattedValue(context As DataGridViewDataErrorContexts) As Object Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
		Return EditingControlFormattedValue
	End Function

	' Implements the
	' IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
	Public Sub ApplyCellStyleToEditingControl(dataGridViewCellStyle As DataGridViewCellStyle) Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
		Me.Font = dataGridViewCellStyle.Font
		Me.CalendarForeColor = dataGridViewCellStyle.ForeColor
		Me.CalendarMonthBackground = dataGridViewCellStyle.BackColor
	End Sub

	' Implements the IDataGridViewEditingControl.EditingControlRowIndex
	' property.
	Public Property EditingControlRowIndex() As Integer Implements IDataGridViewEditingControl.EditingControlRowIndex
		Get
			Return rowIndex
		End Get
		Set
			rowIndex = value
		End Set
	End Property

	' Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
	' method.
	Public Function EditingControlWantsInputKey(key As Keys, 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 False
		End Select
	End Function

	' Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
	' method.
	Public Sub PrepareEditingControlForEdit(selectAll As Boolean) Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
		' No preparation needs to be done.
	End Sub

	' Implements the IDataGridViewEditingControl
	' .RepositionEditingControlOnValueChange property.
	Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange
		Get
			Return False
		End Get
	End Property

	' Implements the IDataGridViewEditingControl
	' .EditingControlDataGridView property.
	Public Property EditingControlDataGridView() As DataGridView Implements IDataGridViewEditingControl.EditingControlDataGridView
		Get
			Return dataGridView
		End Get
		Set
			dataGridView = value
		End Set
	End Property

	' Implements the IDataGridViewEditingControl
	' .EditingControlValueChanged property.
	Public Property EditingControlValueChanged() As Boolean Implements IDataGridViewEditingControl.EditingControlValueChanged
		Get
			Return valueChanged
		End Get
		Set
			valueChanged = value
		End Set
	End Property

	' Implements the IDataGridViewEditingControl
	' .EditingPanelCursor property.
	Public ReadOnly Property EditingPanelCursor() As Cursor Implements IDataGridViewEditingControl.EditingPanelCursor
		Get
			Return MyBase.Cursor
		End Get
	End Property

	Protected Overrides Sub OnValueChanged(eventargs As EventArgs)
		' Notify the DataGridView that the contents of the cell
		' have changed.
		valueChanged = True
		Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
		MyBase.OnValueChanged(eventargs)
	End Sub
End Class

Public Class Form1
	Inherits Form
	Private dataGridView1 As New DataGridView()

	<stathreadattribute> _
	Public Shared Sub Main()
		Application.Run(New Form1())
	End Sub

	Public Sub New()
		Me.dataGridView1.Dock = DockStyle.Fill
		Me.Controls.Add(Me.dataGridView1)
		AddHandler Me.Load, New EventHandler(AddressOf Form1_Load)
		Me.Text = "DataGridView calendar column demo"
	End Sub

	Private Sub Form1_Load(sender As Object, e As EventArgs)
		Dim col As New CalendarColumn()
		Me.dataGridView1.Columns.Add(col)
		Me.dataGridView1.RowCount = 5
		For Each row As DataGridViewRow In Me.dataGridView1.Rows
			row.Cells(0).Value = DateTime.Now
		Next
	End Sub
End Class</stathreadattribute>
Posted
Updated 16-Apr-13 1:13am
v2

1 solution

Better download the complete example. It is UI (Forms) code, but don't expect it to work when simply copying it into one.

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900