//
you're reading...
Computer & Programming

Menampilkan dateTimePicker dalam DataGridView

Apakah teman-teman pengguna Visual Studio 2005 pernah mencoba untuk menampilkan sebuah dateTimePicker dalam sebuah datagridview? Tapi gak nemu2 cara untuk menampilkan DTP (datetimepicker) tersebut di ColumnType dari DGV (datagridview). Saya mencoba untuk berbagi apa yang saya buat. Sebenarnya ini juga pengembangan dari solusi-solusi yang sudah saya temukan di website tutorial lainnya. Untuk mudahnya saya buat menjadi seperti gambar dibawah. Bisa dikembangkan sesuai kebutuhan.🙂

Image

Berikut kode nya:

‘Import System Windows Forms
Imports System
Imports System.Windows.FormsPublic Class Pemesanan
Inherits FormPrivate Sub Pemesanan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
clearDGVPemesanan()
End SubSub clearDGVPemesanan()
Dim col As New GridDateControl()
Me.dgvPembayaran.Columns.Add(col)
Me.dgvPembayaran.Columns(2).HeaderText = “Jatuh Tempo”
End Sub
End Class

‘Membuat Class GridDateControl
Public Class GridDateControl
Inherits DataGridViewColumn

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

Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As DataGridViewCell)

If Not (value Is 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

‘Membuat Class Calender Cell
Public Class CalendarCell
Inherits DataGridViewTextBoxCell

Public Sub New()
‘ Use the short date format.
Me.Style.Format = “dd / MMMM / yyyy”
End Sub

Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
ByVal initialFormattedValue As Object, _
ByVal dataGridViewCellStyle As DataGridViewCellStyle)

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 GetType(CalendarEditingControl)
End Get
End Property

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

‘Membuat 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.Long
End Sub

Public Property EditingControlFormattedValue() As Object _
Implements IDataGridViewEditingControl.EditingControlFormattedValue

Get
Return Me.Value.ToLongDateString()
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

Public Function GetEditingControlFormattedValue(ByVal context _
As DataGridViewDataErrorContexts) As Object _
Implements IDataGridViewEditingControl.GetEditingControlFormattedValue

Return Me.Value.ToLongDateString()

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

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

Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
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)
valueIsChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
MyBase.OnValueChanged(eventargs)
End Sub
End Class

Untuk download project lengkapnya silahkan download disini

Diskusi

7 thoughts on “Menampilkan dateTimePicker dalam DataGridView

  1. Nice Post Bro… ^^
    LANJUTKAN…!!

    Posted by SinyoK | Juni 6, 2012, 3:22 am
  2. thanks,. really help😀

    Posted by indra | Juni 17, 2012, 10:07 am
  3. thank ya

    Posted by toni | Desember 27, 2012, 6:43 am
  4. kalau gune visual basic 6.0 ? how how how🙂

    Posted by zyra | Juli 16, 2013, 4:50 am

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

Arsip

Visitor’s Location

Enter your email address to follow this blog and receive notifications of new posts by email.

Bergabunglah dengan 13 pengikut lainnya

%d blogger menyukai ini: