0

Я пытаюсь сделать gridview столбец Datetimepicker, и я преуспел, , но у меня есть небольшая проблема, когда пользователь редактирует дату в gridview . появляются короткие «11/9/16», но то, что я хочу, это дата и время вместе «11/9/16 2:34:45 AM»DataGridView DateTimePicker Column - добавить поддержку Long Format (Date and the Time)

Это код, я использую:

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 
    Get 
     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)) _ 
      Then 
      Throw New InvalidCastException("Must be a CalendarCell") 
     End If 
     MyBase.CellTemplate = value 

    End Set 
End Property 

End 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, _ 
     dataGridViewCellStyle) 

    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) 
    Else 
     ctl.Value = CType(Me.Value, DateTime) 
    End If 
End Sub 

Public Overrides ReadOnly Property EditType() As Type 
    Get 
     ' Return the type of the editing control 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 

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 

    Get 
     Return Me.Value.ToShortDateString() 
    End Get 

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

И этот код для добавления столбца:

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

source Code

Как я могу сделать это долго со временем.

+0

[Выборочная Формат даты и времени Строки] (https: // MSDN .microsoft.com/en-us/library/8kb3ddd4 (v = vs.110) .aspx) –

+0

@RezaAghaei, где я могу отредактировать свой код, я уже пытался это сделать, но ничего не изменилось, какое изменение - это datetimepicker it self not значение , так что вы можете мне показать. –

+0

@RezaAghaei этот код, этот код с сайта msn, вы можете проверить исходный код на входе в сообщение, есть link.https: //msdn.microsoft.com/en-us/library/7tas5c80.aspx спасибо –

ответ

1

Чтобы показать дату и время в нестандартном формате, вы должны присвоить формат Format объекта DefaultCellStyle. Свойство CustomFormatDateTimePicker полезно для редактирования, но для отображения данных в ячейке вы должны использовать column.DefaultCellStyle.Format.

Например, чтобы показать дату и время, например 2016/09/15 10:31:04 PM, вы должны использовать yyyy/MM/dd h:mm:ss tt в качестве формата.

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

Для получения дополнительной информации о пользовательских форматов даты см:


DataGridView DateTimePicker Колонка

Вот полный исходный код времени даты сборщика с некоторыми исправлениями. Источник был взят из MSDN, и я только что сделал небольшие изменения и небольшое исправление. Изменения были внесены для поддержки Time и длинного формата. Кроме того, исправление было сделано, чтобы предотвратить исключение, когда значение ячейки DBNull.Value:

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 
     Get 
      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)) _ 
       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 = "yyyy/MM/dd h:mm:ss tt" 
    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) 

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

    Public Overrides ReadOnly Property EditType() As Type 
     Get 
      ' Return the type of the editing control 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 dataGridViewControl As DataGridView 
    Private valueIsChanged As Boolean = False 
    Private rowIndexNum As Integer 

    Public Sub New() 
     Me.Format = DateTimePickerFormat.Custom 
     Me.CustomFormat = "yyyy/MM/dd h:mm:ss tt" 
    End Sub 

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

     Get 
      Return Me.Value.ToString("yyyy/MM/dd h:mm:ss tt") 
     End Get 

     Set(ByVal value As Object) 
      Try 
       ' 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)) 
      Catch 
       ' 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.ToString("yyyy/MM/dd h:mm:ss tt") 

    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 

     ' 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 Not dataGridViewWantsInputKey 
     End Select 

    End Function 

    Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _ 
     Implements IDataGridViewEditingControl.PrepareEditingControlForEdit 

     ' No preparation needs to be done. 

    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) 

     ' Notify the DataGridView that the contents of the cell have changed. 
     valueIsChanged = True 
     Me.EditingControlDataGridView.NotifyCurrentCellDirty(True) 
     MyBase.OnValueChanged(eventargs) 

    End Sub 

End Class 

А вот тестовый код:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Dim dt = New DataTable() 
    dt.Columns.Add("Date", GetType(DateTime)) 
    dt.Rows.Add(DateTime.Now) 
    dt.Rows.Add(DateTime.Now) 
    Dim column = New CalendarColumn() 
    column.DataPropertyName = "Date" 
    Me.DataGridView1.Columns.Add(column) 
    Me.DataGridView1.DataSource = dt 
End Sub 
+0

Это работы спасибо –

+0

есть небольшая проблема, когда я редактирую дату показанное время 12:00:00 AM, как это сделать, чтобы показать текущее время, когда я редактирую Дата. –