Custom cell TimePickerCellElement

2 posts, 0 answers
  1. Jose
    Jose avatar
    8 posts
    Member since:
    Jun 2019

    Posted 17 Feb Link to this post

    Hello.

    I'm tring to make a custom cell with radtimepickerelement but something is not working in SetContentCore because any value I set in the timepicker editor when i see the value it's always nothing.

     

    And in the other hand I can't use the custom editor in the row witch is used for add new rows in the grid.

     

    I've done a sample code if you could help me

    Thx.

     

     

      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Dim colnombre As New GridViewTextBoxColumn("nombre")
            Dim colHora As New TimePickerColumn("hora")

            GVPrueba.Columns.Add(colnombre)
            GVPrueba.Columns.Add(colHora)

            GVPrueba.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill


        End Sub

        Private Sub GVPrueba_SelectionChanged(sender As Object, e As EventArgs) Handles GVPrueba.SelectionChanged
            If GVPrueba.SelectedRows.Count = 1 Then

                If GVPrueba.SelectedRows(0).Cells("hora").Value IsNot Nothing Then
                    MsgBox(GVPrueba.SelectedRows(0).Cells("hora").Value.ToString)

                End If
            End If
        End Sub




        Private Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click
            If GVPrueba.Rows.Count > 0 Then
                GVPrueba.Rows(0).Cells("hora").Value = Now
            End If
        End Sub


        Public Class TimePickerCellElement
            Inherits GridDataCellElement

            Public Sub New(ByVal column As GridViewColumn, ByVal row As GridRowElement)
                MyBase.New(column, row)
            End Sub

            Private oRadTimePickerElement As RadTimePickerElement
            Protected Overrides Sub CreateChildElements()
                MyBase.CreateChildElements()
                oRadTimePickerElement = New RadTimePickerElement()
                Me.Children.Add(oRadTimePickerElement)
            End Sub

            Protected Overrides Sub SetContentCore(value As Object)
                If Me.Value IsNot Nothing AndAlso Me.Value IsNot DBNull.Value Then
                    DateTime.TryParse(value, oRadTimePickerElement.Value)
                    '       MyBase.SetContentCore(value)
                End If
            End Sub

            Public Overrides Function IsCompatible(ByVal data As GridViewColumn, ByVal context As Object) As Boolean
                Return TypeOf data Is TimePickerColumn AndAlso (TypeOf context Is GridDataRowElement OrElse TypeOf context Is GridNewRowElement)
            End Function

        End Class

        Public Class TimePickerColumn
            Inherits GridViewDataColumn
            Public Sub New(ByVal fieldName As String)
                MyBase.New(fieldName)
            End Sub

            Public Overrides Function GetCellType(ByVal row As GridViewRowInfo) As Type
                If TypeOf row Is GridViewDataRowInfo Then
                    Return GetType(TimePickerCellElement)
                End If
                Return MyBase.GetCellType(row)
            End Function

        End Class

     


  2. Nadya
    Admin
    Nadya avatar
    189 posts

    Posted 20 Feb Link to this post

    Hello Jose,

    Thank you for the provided code snippet. It seems ok to me. However, I noticed that you do not assign editor and this is why you can not enter edit mode in your custom cell when adding a new row in the grid. You should subscribe to the EditorRequired event, which is triggered when a cell needs an editor and use an appropriate editor. In your case it is suitable to use the GridTimePickerEditor:

    Private Sub RadGridView1_EditorRequired(sender As Object, e As EditorRequiredEventArgs)
        If RadGridView1.CurrentColumn.Name = "hora" Then
            e.EditorType = GetType(GridTimePickerEditor)
        End If
    End Sub
    

    I hope this helps. Let me know if I can assist you further.

    Regards,
    Nadya
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top