RadGridView column with multiple data types - dropdown issue and datepicker issue

2 posts, 0 answers
  1. Simon
    Simon avatar
    13 posts
    Member since:
    Aug 2011

    Posted 02 Nov Link to this post

    Hi

    I have a column with multiple data types. I'm using the EditorRequired event to set the editor as a RadDateTimeEditor or RadDropDownListEditor using the code below. This mostly works but I have two problems. 

    1. With the country dropdown, I set dropDownStyle to DropDown. The editor allows me to type in any value but if the value is not one of the items in the list, as soon as I exit the cell the value disappears.

    2. With the RadDateTimeEditor, I set the customFormat to "d" but when I select a value and exit the cell, it's showing the time as well as the date. How do I get it to show date only?

     

    Private maritalStatuses() As String = {"Married", "In a De Facto Relationship", "Interdependent Relationship", "Engaged", "Separated", "Divorced", "Widowed", "Never Married"}

    Private Countries() as String = {"", "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola"}

     

    Private Sub dgv_EditorRequired(sender As Object, e As EditorRequiredEventArgs) Handles dgv.EditorRequired
            If dgv.CurrentColumn.Name = "ImportValue" Then
                Select Case dgv.CurrentRow.Cells("EditorType").Value
                    Case radEditorType.DatePicker
                        Dim editor As RadDateTimeEditor = New RadDateTimeEditor
                        editor.CustomFormat = "d"
                        e.Editor = editor
                    Case radEditorType.CountryDropDown
                        Dim editor As New RadDropDownListEditor
                        editor.DropDownStyle = RadDropDownStyle.DropDown
                        DirectCast(editor.EditorElement, RadDropDownListEditorElement).DataSource = Countries
                        DirectCast(editor.EditorElement, RadDropDownListEditorElement).AutoCompleteMode = AutoCompleteMode.SuggestAppend
                        e.Editor = editor
                    Case radEditorType.MaritalStatusDropDown
                        Dim editor As New RadDropDownListEditor
                        DirectCast(editor.EditorElement, RadDropDownListEditorElement).DataSource = maritalStatuses
                        editor.DropDownStyle = RadDropDownStyle.DropDownList
                        e.Editor = editor
                End Select
            End If
        End Sub

  2. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 02 Nov Link to this post

    Hello Simon,

    1. This is the default behavior, you can override it by creating a custom editor and return the text:
    Friend Class MyRadDropDownListEditor
        Inherits RadDropDownListEditor
      
        Public Overrides Property Value() As Object
            Get
                Dim editor As RadDropDownListElement = TryCast(Me.EditorElement, RadDropDownListElement)
                If editor.SelectedItem IsNot Nothing Then
                    Return MyBase.Value
                End If
                Return editor.Text
            End Get
      
            Set(ByVal value As Object)
                MyBase.Value = value
            End Set
        End Property
    End Class

    2. You can use the CellFormatting event to change the cell text:
    Private Sub RadGridView1_CellFormatting(ByVal sender As Object, ByVal e As CellFormattingEventArgs)
        If e.Column.Name = "ImportValue" AndAlso e.Row.Cells("EditorType").Value IsNot Nothing Then
            Dim editorType = CType(e.Row.Cells("EditorType").Value, RadEditorType)
            If editorType Is RadEditorType.DatePicker Then
                e.CellElement.Text = Convert.ToDateTime(e.CellElement.Value).ToShortDateString()
            End If
        End If
    End Sub

    I hope this will be useful. Let me know if you have additional questions.

    Regards,
    Dimitar
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top