0

Ich versuche, Gridview Datetimepicker Spalte und ich erfolgreich, , aber ich habe ein kleines Problem, dass, wenn der Benutzer das Datum in der Gridview das Datum bearbeiten erscheint kurz „11/9/16“ aber was ich will, ist das Datum und die Zeit zusammen „11/9/16 02.34.45“DataGridView DateTimePicker Spalte - hinzufügen Langformat Unterstützung (Datum und die Zeit)

Dies ist der Code ich benutze:

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 

Und dieser Code für Add Spalte:

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

source Code

Wie kann ich es mit der Zeit lang.

+0

[Custom Datum und Uhrzeit Format Strings] (https: // msdn .microsoft.com/de-us/library/8kb3ddd4 (v = vs.110) .aspx) –

+0

@RezaAghaei wo kann ich meinen Code bearbeiten, ich habe schon versucht, dies zu tun, aber nichts zu ändern, welche Änderung ist das Datetimepicker es selbst nicht der Wert so kannst du mir bitte zeigen. –

+0

@RezaAghaei das alles Code, diesen Code von msn Website können Sie den Quellcode am Eingang des Beitrags überprüfen, es gibt einen Link.https: //msdn.microsoft.com/en-us/library/7tas5c80.aspx danke –

Antwort

1

Um ein Datum und eine Uhrzeit im benutzerdefinierten Format anzuzeigen, sollten Sie das Format Format der Eigenschaft DefaultCellStyle zuweisen. Die CustomFormat Eigenschaft von DateTimePicker ist nützlich für das Bearbeiten von Format, aber für die Anzeige von Daten in der Zelle sollten Sie column.DefaultCellStyle.Format verwenden.

Um beispielsweise Datum und Uhrzeit wie 2016/09/15 10:31:04 PM anzuzeigen, sollten Sie yyyy/MM/dd h:mm:ss tt als Format verwenden.

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 

Weitere Informationen zu benutzerdefinierten Datumsformate finden Sie unter:


Datagridview Datetime Spalte

Hier ist der vollständige Quellcode Datum Zeit Auswahlspalte mit einigen Korrekturen. Die Quelle wurde von MSDN genommen und ich habe nur ein paar kleine Änderungen und ein kleines Update vorgenommen. Die Änderungen wurden vorgenommen, um Time und langes Format zu unterstützen. Auch das Update wurde getan Ausnahme zu verhindern, wenn der Zellwert ist 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 

Und hier ist der Testcode:

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

Dies ist funktioniert, danke –

+0

gibt es ein kleines Problem, wenn ich das Datum bearbeiten die angezeigte Zeit ist 12:00:00 Uhr, wie es zu tun, um die aktuelle Zeit zu zeigen, wenn ich bearbeite das Datum. –

Verwandte Themen