Rabeea qabaha Rabeea qabaha - 1 year ago 93
Vb.net Question

gridview Datetimepicker column long format (Date and the Time)

I'm trying to make gridview Datetimepicker column and i succeeded,
but i have a little problem that when the user edit the date in the gridview
the date appear in short "11/9/16" but what i want is the date and the time together"11/9/16 2:34:45 AM"

this is the code i use:

CLASS CalendarColumn :

Imports System
Imports System.Windows.Forms

Public Class CalendarColumn
Inherits DataGridViewColumn

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

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

' Ensure that the cell used for the template is a CalendarCell.
If (value IsNot Nothing) AndAlso _
Not value.GetType().IsAssignableFrom(GetType(CalendarCell)) _
Throw New InvalidCastException("Must be a CalendarCell")
End If
MyBase.CellTemplate = value

End Set
End Property

End Class

CLASS CalendarCell:

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, _

Dim ctl As CalendarEditingControl = _
CType(DataGridView.EditingControl, CalendarEditingControl)

' Use the default row value when Value property is null.
If (Me.Value Is Nothing) Then
ctl.Value = CType(Me.DefaultNewRowValue, DateTime)
ctl.Value = CType(Me.Value, DateTime)
End If
End Sub

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

Public Overrides ReadOnly Property ValueType() As Type
' Return the type of the value that CalendarCell contains.
Return GetType(DateTime)
End Get
End Property

Public Overrides ReadOnly Property DefaultNewRowValue() As Object
' Use the current date and time as the default value.
Return DateTime.Now
End Get
End Property

End Class

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
'Me.CustomFormat = "'Today is:' hh:mm:ss dddd MMMM dd, yyyy"
End Sub

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

Return Me.Value.ToShortDateString()
End Get

Set(ByVal value As Object)
' 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))
' 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

and this code for fill column :

Dim col As New CalendarColumn() With {.HeaderText = "Date time"}
DataGridView1.Columns.Insert(5, col)

source Code

how can i make it long with the time

Answer Source

To show a date and time in custom format, you should assign format to Format property of DefaultCellStyle. The CustomFormat property of DateTimePicker is useful for editing format but for displaying data in cell you should use column.DefaultCellStyle.Format.

For example to show date and time like 2016/09/15 10:31:04 PM you should use yyyy/MM/dd h:mm:ss tt as format.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim dt = New DataTable()
    dt.Columns.Add("Date", GetType(DateTime))
    Dim column = New DataGridViewTextBoxColumn()
    column.DefaultCellStyle.Format = "yyyy/MM/dd h:mm:ss tt"
    column.DataPropertyName = "Date"
    Me.DataGridView1.DataSource = dt
End Sub

For more information about custom date formats see: