Add Datetime picker in datagrid view using VB.NET

In this article you will learn how can we add datetime picker in datagridcell.
  • 33827
 

Add Class file in your project and Inherit it From DataGridViewColumn

Make CalendarCell Class your main class because we will use more than one class in this File

Public Sub New()

        MyBase.New(New CalendarCell())

End Sub

Override CellTemplate property So then we can set Cell in data Grid View.

    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 CalendarCell.

            If Not (value Is NothingAndAlso _

                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()

        ' Use the short date format.

        Me.Style.Format = "d"

    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 CalendarEditingControl = _

            CType(DataGridView.EditingControl, CalendarEditingControl)

        If Not Me.Value Is DBNull.Value Then

            If Not Me.Value Is Nothing Then

                ctl.Value = CType(Me.Value, DateTime)

            End If

        End If

    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

End Class

 

Inheri Datetime Picker with class CalendarEditingControl

 

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

   

Set Date format of Datetime

 

    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

 

Get Datetime value from Control

 

    Public Function GetEditingControlFormattedValue(ByVal context _

        As DataGridViewDataErrorContexts) As Object _

        Implements IDataGridViewEditingControl.GetEditingControlFormattedValue

 

        Return Me.Value.ToShortDateString()

 

    End Function

 

Set datagrid Cell style In which you want to Get datetime pickre

   

Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As _

        DataGridViewCellStyle) _

        Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl

 

        Me.Font = dataGridViewCellStyle.Font

        Me.CalendarForeColor = dataGridViewCellStyle.ForeColor

        Me.CalendarMonthBackground = dataGridViewCellStyle.BackColor

 

    End Sub

 

 

Get Row index of Datagrid view in which you are using datetime picker

 

    Public Property EditingControlRowIndex() As Integer _

        Implements IDataGridViewEditingControl.EditingControlRowIndex

 

        Get

            Return rowIndexNum

        End Get

        Set(ByVal value As Integer)

            rowIndexNum = value

        End Set

 

    End Property

 

Set input keys functionality

 

    Public Function EditingControlWantsInputKey(ByVal key As Keys, _

        ByVal dataGridViewWantsInputKey As BooleanAs 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

 

 

Set edited value in Datagrid cell

 

    Public Property EditingControlDataGridView() As DataGridView _

        Implements IDataGridViewEditingControl.EditingControlDataGridView

 

        Get

            Return dataGridViewControl

        End Get

        Set(ByVal value As DataGridView)

            dataGridViewControl = value

        End Set

 

    End Property

 

Set edited value in value change

 

    Public Property EditingControlValueChanged() As Boolean _

        Implements IDataGridViewEditingControl.EditingControlValueChanged

 

        Get

            Return valueIsChanged

        End Get

        Set(ByVal value As Boolean)

            valueIsChanged = value

        End Set

 

    End Property

 

 

Set Cursor

 

 

    Public ReadOnly Property EditingControlCursor() As Cursor _

        Implements IDataGridViewEditingControl.EditingPanelCursor

 

        Get

            Return MyBase.Cursor

        End Get

 

    End Property

And at last you will find this Cell type in your datagridview as Columntype property.

DatePickerInDataGridCell.JPG

 

 You Can Download also the complete source code

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.