Completed
Last Updated: 01 Dec 2015 07:55 by ADMIN
ADMIN
Dess | Tech Support Engineer, Sr.
Created on: 03 Nov 2015 07:34
Category: GridView
Type: Bug Report
0
FIX. RadGridView - FormatException when pasting an empty string in a GridViewDateTimeColumn
To reproduce: 

1. Bind RadGridView to a collection of business objects where one of the properties is Nullable<DateTime>.
2. Leave one of the items with an empty date (null value).
3. Copy the entire row and try to paste in one of the grid rows. The FormatException is thrown.

Sub New()
    InitializeComponent()

    Dim items As New List(Of Item)

    items.Add(New Item(1, DateTime.Now.AddDays(2), "Item1"))
    items.Add(New Item(2, Nothing, "Item2"))
    items.Add(New Item(3, DateTime.Now.AddDays(4), "Item3"))
    items.Add(New Item(4, DateTime.Now.AddDays(5), "Item4"))

    Me.RadGridView1.DataSource = items
    Me.RadGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill 
End Sub

Public Class Item
    Private _id As Integer
    Private _createdOn As Nullable(Of DateTime)
    Private _title As String

    Public Sub New(id As Integer, createdOn As Nullable(Of DateTime), title As String)
        Me._id = id
        Me._createdOn = createdOn
        Me._title = title
    End Sub

    Public Property Id() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Public Property CreatedOn() As Nullable(Of DateTime)
        Get
            Return _createdOn
        End Get
        Set(ByVal value As Nullable(Of DateTime))
            _createdOn = value
        End Set
    End Property

    Public Property Title() As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property
End Class

Workaround: use a TypeConverter

Sub New()
    InitializeComponent()

    Dim items As New List(Of Item)

    items.Add(New Item(1, DateTime.Now.AddDays(2), "Item1"))
    items.Add(New Item(2, Nothing, "Item2"))
    items.Add(New Item(3, DateTime.Now.AddDays(4), "Item3"))
    items.Add(New Item(4, DateTime.Now.AddDays(5), "Item4"))

    Me.RadGridView1.DataSource = items
    Me.RadGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill
    DirectCast(Me.RadGridView1.Columns(1), GridViewDateTimeColumn).DataTypeConverter=New NullableDateTimeConverter()
End Sub

Public Class NullableDateTimeConverter
Inherits TypeConverter
   
    Public Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
        Return sourceType.Equals(GetType(String))
    End Function

    Public Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object) As Object
        Dim parsedDate As DateTime
        If DateTime.TryParse(value.ToString(), parsedDate) Then
            Return parsedDate
        End If 
        Return Nothing
    End Function
End Class

Public Class Item
    Private _id As Integer
    Private _createdOn As Nullable(Of DateTime)
    Private _title As String

    Public Sub New(id As Integer, createdOn As Nullable(Of DateTime), title As String)
        Me._id = id
        Me._createdOn = createdOn
        Me._title = title
    End Sub

    Public Property Id() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Public Property CreatedOn() As Nullable(Of DateTime)
        Get
            Return _createdOn
        End Get
        Set(ByVal value As Nullable(Of DateTime))
            _createdOn = value
        End Set
    End Property

    Public Property Title() As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property
End Class

0 comments