Avatar of kidl33t
kidl33t
Flag for Canada

asked on 

Custom DataGridView Time column not accepting changes?

Hello all,

I have a custom dataGridView time column, which is based on the Microsoft DateTimePicker column demo.  The control appears fine.  The column is there, and when you click into it, the spin control shows allowing you change the date/time etc.  The problem is that when you navigate to another cell, the time doesn't stay saved in the cell.  It reverts to 12:00  I will post the whole class I am using in the code section below, and after that how I am instantiating it.  It is based on the Microsoft demo of the datetime picker column.  The link to that is here:

http://msdn.microsoft.com/en-us/netframework/7tas5c80.aspx

All I really changed was a few of the class names.  I changed the New() sub to:
    Public Sub New()
        ' Use the short date format.
        Me.Style.Format = "hh:mm:ss:tt"
        Me.Value = Date.Today
    End Sub
From:
    Public Sub New()
        ' Use the short date format.
        Me.Style.Format = "d"
    End Sub

And I added the line:
        Me.ctl.ShowUpDown = True
To the Public Overrides Sub InitializeEditingControl().

Any ideas?  I really need this to work properly.  I also need to be able to get the time portion of the value from the cell.  Thanks,

Imports System
Imports System.Windows.Forms
 
Public Class RM_TimeColumn
    Inherits DataGridViewColumn
 
    Public Sub New()
        MyBase.New(New TimeCell())
    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 TimeCell.
            If (value IsNot Nothing) AndAlso _
                Not value.GetType().IsAssignableFrom(GetType(TimeCell)) _
                Then
                Throw New InvalidCastException("Must be a TimeCell")
            End If
            MyBase.CellTemplate = value
 
        End Set
    End Property
 
End Class
 
Public Class TimeCell
    Inherits DataGridViewTextBoxCell
 
    'Public WithEvents CalendarEditingControl As CalendarEditingControl
    Private ctl As CalendarEditingControl
 
    Public Sub New()
        ' Use the short date format.
        Me.Style.Format = "hh:mm:ss:tt"
        Me.Value = Date.Today
    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)
 
        Me.ctl = _
            CType(DataGridView.EditingControl, CalendarEditingControl)
        ctl.Value = CType(Me.Value, DateTime)
        Me.ctl.Format = DateTimePickerFormat.Time
        Me.ctl.ShowUpDown = True
    End Sub
 
    Public Overrides ReadOnly Property EditType() As Type
        Get
            ' Return the type of the editing contol that TimeCell uses.
            Return GetType(CalendarEditingControl)
        End Get
    End Property
 
    Public Overrides ReadOnly Property ValueType() As Type
        Get
            ' Return the type of the value that TimeCell 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.Short
    End Sub
 
    Public Property EditingControlFormattedValue() As Object _
        Implements IDataGridViewEditingControl.EditingControlFormattedValue
 
        Get
            Return Me.Value.ToShortDateString()
        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.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
 
    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 False
        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
 
    Private Sub CalendarEditingControl_CloseUp(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.CloseUp
        If Me.valueIsChanged = True Then
            'Console.WriteLine(sender.Value)
            Me.valueIsChanged = False
        End If
    End Sub
End Class
 
'######################################################################
'This is how I am using it
 
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim newCol As DataGridViewColumn
        Dim newCell As DataGridViewCell
        newCol = New RM_TimeColumn
        newCell = New TimeCell
        newCol.CellTemplate = newcell
        newCol.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        With newCol
            .Name = "Time"
            .HeaderText = "Time Test"
        End With
        Me.DataGridView1.Columns.Add(newCol)
End Sub

Open in new window

.NET ProgrammingProgramming Languages-OtherVisual Basic.NET

Avatar of undefined
Last Comment
Jorge Paulino

8/22/2022 - Mon