DateTimeOffset

6 posts, 1 answers
  1. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 09 Dec 2011 Link to this post

    We continue to have problems with using a GridViewDateTimeColumn with DateTimeOffset. I *thought* we had this working months ago, but revisited a feature that we have not used for many months and we cannot get it to work. Don't know if it is due to the version of the controls we are using? Or something we have done.

    We continue to either get "Converter can't convert" or other types of formatting errors. To help demonstrate this problem, we did a very simple sample. Just try to drop down one of the calendar drop downs and the error occurs.

    Here is the code:

    Imports System.ComponentModel
    Imports Telerik.WinControls.UI
      
    Public Class Form1
      
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            TimeCardGrid.MasterTemplate.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill
            TimeCardBindingSource.DataSource = TimeCards.Retrieve()
        End Sub
      
    End Class
      
    Public Class TimeCard
        Public Property StartTime As DateTimeOffset?
        Public Property endTime As DateTimeOffset?
    End Class
      
    Public Class TimeCards
        Inherits BindingList(Of TimeCard)
      
        Public Shared Function Retrieve() As TimeCards
            Dim timeCardList As New TimeCards From
                    {New TimeCard With {.StartTime = New DateTimeOffset(2011, 12, 8, 9, 45, 30, New TimeSpan(-8, 0, 0)),
                                        .endTime = New DateTimeOffset(2011, 12, 9, 11, 45, 30, New TimeSpan(-8, 0, 0))},
                     New TimeCard With {.StartTime = New DateTimeOffset(2011, 12, 11, 9, 45, 30, New TimeSpan(-8, 0, 0)),
                                        .endTime = New DateTimeOffset(2011, 12, 12, 11, 45, 30, New TimeSpan(-8, 0, 0))}}
            Return timeCardList
        End Function
    End Class
      
    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Partial Class Form1
        Inherits System.Windows.Forms.Form
      
        'Form overrides dispose to clean up the component list.
        <System.Diagnostics.DebuggerNonUserCode()> _
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                If disposing AndAlso components IsNot Nothing Then
                    components.Dispose()
                End If
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub
      
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
      
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()
            Me.components = New System.ComponentModel.Container()
            Dim GridViewDateTimeColumn1 As Telerik.WinControls.UI.GridViewDateTimeColumn = New Telerik.WinControls.UI.GridViewDateTimeColumn()
            Dim GridViewDateTimeColumn2 As Telerik.WinControls.UI.GridViewDateTimeColumn = New Telerik.WinControls.UI.GridViewDateTimeColumn()
            Me.TimeCardBindingSource = New System.Windows.Forms.BindingSource(Me.components)
            Me.TimeCardGrid = New Telerik.WinControls.UI.RadGridView()
            CType(Me.TimeCardBindingSource, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.TimeCardGrid, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            '
            'TimeCardBindingSource
            '
            Me.TimeCardBindingSource.DataSource = GetType(WindowsApplication9.TimeCard)
            '
            'TimeCardGrid
            '
            Me.TimeCardGrid.Location = New System.Drawing.Point(12, 12)
            '
            'TimeCardGrid
            '
            Me.TimeCardGrid.MasterTemplate.AllowAddNewRow = False
            Me.TimeCardGrid.MasterTemplate.AllowColumnChooser = False
            Me.TimeCardGrid.MasterTemplate.AllowColumnReorder = False
            Me.TimeCardGrid.MasterTemplate.AllowDeleteRow = False
            Me.TimeCardGrid.MasterTemplate.AllowDragToGroup = False
            Me.TimeCardGrid.MasterTemplate.AutoGenerateColumns = False
            GridViewDateTimeColumn1.DataType = GetType(System.Nullable(Of System.DateTimeOffset))
            GridViewDateTimeColumn1.FieldName = "StartTime"
            GridViewDateTimeColumn1.Format = System.Windows.Forms.DateTimePickerFormat.[Short]
            GridViewDateTimeColumn1.FormatString = "{0:d}"
            GridViewDateTimeColumn1.HeaderText = "StartTime"
            GridViewDateTimeColumn1.Name = "StartTime"
            GridViewDateTimeColumn2.DataType = GetType(System.Nullable(Of System.DateTimeOffset))
            GridViewDateTimeColumn2.FieldName = "EndTime"
            GridViewDateTimeColumn2.Format = System.Windows.Forms.DateTimePickerFormat.[Short]
            GridViewDateTimeColumn2.FormatString = "{0:d}"
            GridViewDateTimeColumn2.HeaderText = "EndTime"
            GridViewDateTimeColumn2.Name = "EndTime"
            Me.TimeCardGrid.MasterTemplate.Columns.AddRange(New Telerik.WinControls.UI.GridViewDataColumn() {GridViewDateTimeColumn1, GridViewDateTimeColumn2})
            Me.TimeCardGrid.MasterTemplate.DataSource = Me.TimeCardBindingSource
            Me.TimeCardGrid.MasterTemplate.ShowFilteringRow = False
            Me.TimeCardGrid.MasterTemplate.ShowRowHeaderColumn = False
            Me.TimeCardGrid.Name = "TimeCardGrid"
            Me.TimeCardGrid.ShowGroupPanel = False
            Me.TimeCardGrid.Size = New System.Drawing.Size(315, 235)
            Me.TimeCardGrid.TabIndex = 2
            Me.TimeCardGrid.Text = "RadGridView1"
            '
            'Form1
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(347, 263)
            Me.Controls.Add(Me.TimeCardGrid)
            Me.Name = "Form1"
            Me.Text = "Form1"
            CType(Me.TimeCardBindingSource, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.TimeCardGrid, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)
      
        End Sub
        Friend WithEvents TimeCardBindingSource As System.Windows.Forms.BindingSource
        Friend WithEvents TimeCardGrid As Telerik.WinControls.UI.RadGridView
      
    End Class


    Please let us know what we need to do to get this to work.

    THANKS!
  2. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 13 Dec 2011 Link to this post

    Hello Deborah,

    Thank you for writing back.

    It seems that our Grid Conversion Layer cannot covert DateTimeOffset? to DateTime.
    I would like to suggest to create a custom editor and convert manually DateTimeOffset? to DateTime.
    1. You should handle EditorRequired event 
    2. Create MyDateTimeEditor that inherits from RadDateTimeEditor
    3. Override sub Initialize
    Please refer to the code below:
    Private Sub TimeCardGrid_EditorRequired( sender As System.Object,  e As Telerik.WinControls.UI.EditorRequiredEventArgs) Handles TimeCardGrid.EditorRequired
            If e.EditorType Is GetType(RadDateTimeEditor) Then
            e.EditorType = GetType(MyDateTimeEditor)
        End If
    End Sub
     
    Public Class MyDateTimeEditor
        Inherits RadDateTimeEditor
        Overrides Sub Initialize( owner as object,  value as object)
            me.Value = DirectCast(value, DateTimeOffset?).Value.DateTime'here is the 'cast from DateTimeOffset to DateTime
     End Sub
    End Class

    A bit off topic, I would like to remind you that your subscription package has expired, which means that you can be denied support services. I would highly recommend upgrading your license to the latest version in order to continue getting product updates and uninterrupted support services. Please, contact sales@telerik.com or visit your account for your upgrading options.

    I hope this helps.

    Kind regards,
    Peter
    the Telerik team

    Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.

  3. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 13 Dec 2011 Link to this post

    Hi Peter -

    Thank you for your reply. (I have contacted your sales team.)

    Your solution got me one step further, but still does not work completely. I can now drop down the calendar and select a date. But when I move off the field, I get the conversion error in the other direction ("Invalid cast from 'System.DateTime' to 'System.DateTimeOffset'"). I assume that I need some code in some other event to convert the datetime back to a datetimeoffset?

    Thanks -
  4. Answer
    Peter
    Admin
    Peter avatar
    1148 posts

    Posted 16 Dec 2011 Link to this post

    Hi Deborah,

    Thank you for the provided information.

    It seems that you should also override the Value property:

        Public Overrides Property Value() As Object
        Get    
            Return new DateTimeOffset(DirectCast(mybase.Value, DateTime))
        End Get
        Set
            mybase.Value = DirectCast(value, DateTime)
        End Set
    End Property

    Please refer to the complete custom editor code below:
    public class MyDateTimeEditor
        inherits RadDateTimeEditor
        Overrides sub Initialize( owner as object,  value as object)
            me.Value = DirectCast(value, DateTimeOffset?).Value.DateTime
        End Sub
     
        Public Overrides Property Value() As Object
        Get    
            Return new DateTimeOffset(DirectCast(mybase.Value, DateTime))
        End Get
        Set
            mybase.Value = DirectCast(value, DateTime)
        End Set
    End Property
     
    End Class

    I hope this helps.

    Kind regards,
    Peter
    the Telerik team

    Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.

  5. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 16 Dec 2011 Link to this post

    That fixed it. Thanks!

    Because I am using a nullable DateTimeOffset, I had to change the code as follows:

    Public Class DateTimeOffsetEditor
        Inherits RadDateTimeEditor
      
        Public Overrides Property Value() As Object
            Get
                If MyBase.Value Is Nothing Then
                    Return MyBase.Value
                Else
                    Return New DateTimeOffset?(DirectCast(MyBase.Value, DateTime))
                End If
            End Get
            Set(value As Object)
                If value Is Nothing Then
                    MyBase.Value = value
                Else
                    MyBase.Value = DirectCast(value, DateTime)
                End If
            End Set
        End Property
      
        ' Needed because the Telerik Grid Conversion Layer cannot convert DateTimeOffset? to DateTime.
        Public Overrides Sub Initialize(owner As Object, value As Object)
            If value Is Nothing Then
                Me.Value = Nothing
            Else
                Me.Value = DirectCast(value, DateTimeOffset?).Value.DateTime
            End If
        End Sub
      
    End Class
  6. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 21 Dec 2011 Link to this post

    Hi Deborah,

    I am happy that you managed to resolve this case.
    Do not hesitate to write back in case you need further help.

    All the best,
    Peter
    the Telerik teamQ3’11 of RadControls for WinForms is available for download (see what's new). Get it today.
Back to Top