Winforms RadGridView DateTimeColumn DateFormat

2 Answers 77 Views
GridView
Jacek
Top achievements
Rank 2
Iron
Iron
Iron
Jacek asked on 28 Sep 2022, 06:28 AM

Hello,
In RadGridView, I set the column to GridViewDateTimeColumn.
I want to change the date display format to short.
Code below.

Public Class RadForm5
    Private Sub RadForm5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim listTest As New List(Of TestDate)
        listTest.Add(New TestDate With {.Name = "John", .DateStart = New Date(2022, 5, 1)})
        listTest.Add(New TestDate With {.Name = "Arya", .DateStart = New Date(2022, 5, 2)})
        listTest.Add(New TestDate With {.Name = "Brandon", .DateStart = New Date(2022, 5, 3)})
        listTest.Add(New TestDate With {.Name = "Catelyn", .DateStart = New Date(2022, 5, 4)})

        For Each col In RadGridView1.Columns
            If col.Name = "colDate" Then
                col.FieldName = "DateStart"
                Dim colDate As GridViewDateTimeColumn = TryCast(col, GridViewDateTimeColumn)
                colDate.Format = DateTimePickerFormat.Short
            End If
        Next

        RadGridView1.DataSource = listTest
    End Sub
End Class

Public Class TestDate
    Public Property Name As String
    Public Property DateStart As Date
    Public Sub New()

    End Sub
End Class


Unfortunately, setting the display format does not work properly.
I have to add more fields in object every time

   Public ReadOnly Property DateStartStr As String
        Get
            Return DateStart.ToShortDateString
        End Get
    End Property

And

col.FieldName = "DateStartStr"

Is there another way to do this?

 

Regards

Jack

2 Answers, 1 is accepted

Sort by
0
Maria
Telerik team
answered on 29 Sep 2022, 12:57 PM

Hello Jacek,

Thank you for the code snippet.

To change the date display format to short you can use the FormatString property in the RadGridView. I change your code a little. In addition, you have to set the List collection before For Each loop because the RadGridView generates the columns when you bind it with the data source.

Here is the code snippet with my changes:

Public Class RadForm1
    Private Sub RadForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim listTest As New List(Of TestDate)

        listTest.Add(New TestDate With {.Name = "John", .DateStart = New Date(2022, 5, 1)})
        listTest.Add(New TestDate With {.Name = "Arya", .DateStart = New Date(2022, 5, 2)})
        listTest.Add(New TestDate With {.Name = "Brandon", .DateStart = New Date(2022, 5, 3)})
        listTest.Add(New TestDate With {.Name = "Catelyn", .DateStart = New Date(2022, 5, 4)})
        RadGridView1.DataSource = listTest
        For Each col In RadGridView1.Columns
            If col.Name = "DateStart" Then
                col.FieldName = "DateStart"
                Dim colDate As GridViewDateTimeColumn = TryCast(col, GridViewDateTimeColumn)
                colDate.FormatString = "{0:MM/dd/yyyy}"
            End If
        Next
    End Sub
End Class

Public Class TestDate
    Public Property Name As String
    Public Property DateStart As Date
    Public Sub New()

    End Sub
End Class

I am sending an image of the form to see the date format and sample project as well.

I hope this works for you.

Regards,
Maria
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Jacek
Top achievements
Rank 2
Iron
Iron
Iron
answered on 29 Sep 2022, 01:14 PM

Thank you for the code snippet.

 

Regards

Jack

Tags
GridView
Asked by
Jacek
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Maria
Telerik team
Jacek
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or