Selasa, 14 April 2015

menampilkan kalender dalam datagridview vb.net



Seringkali kita membutuhkan data tanggal sejenis datetimepicker dalam sebuah datagridview. Untuk itu marilah kita coba membuatnya dengan langkah-langkah sebagai berikut :

1.      Buatlah sebuah project baru
2.      Buatlah sebuah form dan tambahkan sebuah datagridview


3.      Lalu ketiklah atau copy coding di bawah ini :

ImportsSystem.Data.OleDb
ImportsSystem
ImportsSystem.Windows.Forms

Public Class Form1

    Sub KolomBaru()
        Dim ColumnName1, ColumnName2 AsNew CalendarColumn()
        DGV.Columns.Add("KodeKamar", "Kode Kamar")
        ColumnName1.HeaderText = "Tgl Masuk"
        DGV.Columns.Add(ColumnName1)
        ColumnName2.HeaderText = "Tgl Keluar"
        DGV.Columns.Add(ColumnName2)
    End Sub

    Private SubForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
        Call KolomBaru()
    End Sub

    Public ClassCalendarColumn
        Inherits DataGridViewColumn

        Public Sub New()
            MyBase.New(NewCalendarCell())
        End Sub

        Public Overrides Property CellTemplate() AsDataGridViewCell
            Get
                Return MyBase.CellTemplate
            End Get
            Set(ByVal value As DataGridViewCell)
                If (value IsNot Nothing) AndAlso Not value.GetType().IsAssignableFrom(GetType(CalendarCell)) Then
                    Throw NewInvalidCastException("Must be a CalendarCell")
                End If
                MyBase.CellTemplate = value
            End Set
        End Property
    End Class

    Public ClassCalendarCell
        Inherits DataGridViewTextBoxCell

        Public Sub New()
            Me.Style.Format = "d"
        End Sub

        Public Overrides Sub InitializeEditingControl(ByValrowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle AsDataGridViewCellStyle)
            On Error Resume Next
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
            Dim ctl AsCalendarEditingControl = CType(DataGridView.EditingControl, CalendarEditingControl)
            If (Me.Value Is Nothing) Then
                ctl.Value = CType(Me.DefaultNewRowValue, DateTime)
            Else
                ctl.Value = CType(Me.Value, DateTime)
            End If
        End Sub

        Public Overrides ReadOnly PropertyEditType() As Type
            Get
                Return GetType(CalendarEditingControl)
            End Get
        End Property

        Public Overrides ReadOnly PropertyValueType() As Type
            Get
                Return GetType(DateTime)
            End Get
        End Property

        Public Overrides ReadOnly PropertyDefaultNewRowValue() As Object
            Get
                Return DateTime.Now
            End Get
        End Property
    End Class

    Public ClassCalendarEditingControl
        Inherits DateTimePicker
        Implements IDataGridViewEditingControl

        Private dataGridViewControl AsDataGridView
        Private valueIsChanged AsBoolean = False
        Private rowIndexNum AsInteger

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

        Public PropertyEditingControlFormattedValue() As Object ImplementsIDataGridViewEditingControl.EditingControlFormattedValue
            Get
                Return Me.Value.ToShortDateString()
            End Get
            Set(ByVal value As Object)
                Try
                    Me.Value = DateTime.Parse(CStr(value))
                Catch
                    Me.Value = DateTime.Now
                End Try
            End Set
        End Property

        Public FunctionGetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object _
            Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
            Return Me.Value.ToShortDateString()
        End Function

        Public SubApplyCellStyleToEditingControl(ByValdataGridViewCellStyle As DataGridViewCellStyle) ImplementsIDataGridViewEditingControl.ApplyCellStyleToEditingControl
            Me.Font = dataGridViewCellStyle.Font
            Me.CalendarForeColor = dataGridViewCellStyle.ForeColor
            Me.CalendarMonthBackground = dataGridViewCellStyle.BackColor
        End Sub

        Public PropertyEditingControlRowIndex() As Integer ImplementsIDataGridViewEditingControl.EditingControlRowIndex
            Get
                Return rowIndexNum
            End Get
            Set(ByVal value As Integer)
                rowIndexNum = value
            End Set
        End Property

        Public FunctionEditingControlWantsInputKey(ByVal key As Keys, ByValdataGridViewWantsInputKey As Boolean) As Boolean ImplementsIDataGridViewEditingControl.EditingControlWantsInputKey
            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 NotdataGridViewWantsInputKey
            End Select
        End Function

        Public SubPrepareEditingControlForEdit(ByVal selectAll As Boolean) ImplementsIDataGridViewEditingControl.PrepareEditingControlForEdit

        End Sub

        Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean ImplementsIDataGridViewEditingControl.RepositionEditingControlOnValueChange
            Get
                Return False
            End Get
        End Property

        Public PropertyEditingControlDataGridView() As DataGridView ImplementsIDataGridViewEditingControl.EditingControlDataGridView
            Get
                Return dataGridViewControl
            End Get
            Set(ByVal value As DataGridView)
                dataGridViewControl = value
            End Set
        End Property

        Public PropertyEditingControlValueChanged() As Boolean ImplementsIDataGridViewEditingControl.EditingControlValueChanged
            Get
                Return valueIsChanged
            End Get
            Set(ByVal value As Boolean)
                valueIsChanged = value
            End Set
        End Property

        Public ReadOnly Property EditingControlCursor() As Cursor ImplementsIDataGridViewEditingControl.EditingPanelCursor
            Get
                Return MyBase.Cursor
            End Get
        End Property

        Protected Overrides Sub OnValueChanged(ByValeventargs As EventArgs)
            valueIsChanged = True
            Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
            MyBase.OnValueChanged(eventargs)
        End Sub
    End Class
End Class

Hasil akhir dari coding tersebut adalah sebagai berikut :




Tidak ada komentar:

Posting Komentar