Click here to Skip to main content
15,359,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to change the date time format of datagridview1 but is it not work please help me

change date time format no 6 to No 11 column format is
dd/MMM/yyyy hh:mm:ss tt"

My Code is
Private Sub loadfiles_1()
        Dim dt As New DataTable
        dt.Columns.Add("Full Name")
        dt.Columns.Add("Creation Time")
        dt.Columns.Add("Creation Time Utc")
        dt.Columns.Add("LastAccess Time")
        dt.Columns.Add("LastAccess Time Utc")
        dt.Columns.Add("Last Write Time")
        dt.Columns.Add("Last WriteTime Utc")

        '  Dim directory = Label3.Text
        Dim dirinfo As New DirectoryInfo(Label3.Text)

            Dim files As System.IO.FileInfo() = Nothing
            For Each directory In dirinfo.GetDirectories()
                    If (directory.Attributes <> FileAttributes.System) Then

                        files = directory.GetFiles("*", SearchOption.AllDirectories)

                    End If

                    For Each File In files
                        Dim dr As DataRow = dt.NewRow()
                        dr(0) = File.Name
                        dr(1) = File.FullName
                        dr(2) = File.DirectoryName
                        dr(3) = File.Exists
                        dr(4) = File.Directory
                        dr(5) = File.Extension
                        dr(6) = File.CreationTime
                        dr(7) = File.CreationTimeUtc
                        dr(8) = File.LastAccessTime
                        dr(9) = File.LastAccessTimeUtc
                        dr(10) = File.LastWriteTime
                        dr(11) = File.LastWriteTimeUtc
                        dr(12) = File.Attributes
                        dr(13) = File.Length
                        dr(14) = File.IsReadOnly
                Catch ex As Exception
                End Try
        Catch ex As UnauthorizedAccessException
        End Try
        DataGridView1.DataSource = dt
    End Sub

What I have tried:

DataGridView1.Columns(6).DefaultCellStyle.Format = "dd/MMM/yyyy hh:mm:ss"
Updated 15-May-22 22:25pm
Richard MacCutchan 16-May-22 3:55am
"but is it not work please help me"
How can we help if you do not explain what the problem is? Saying, "it is not working", tells us nothing.
Jayanta Modak 16-May-22 6:54am
thanks sir for reply actually the problem is i want to fix the date time format "dd/MMM/yyyy hh:mm:ss"
but when I try my tried code nothing do change
Richard MacCutchan 16-May-22 6:58am
Try what Richard Deeming suggests below.
Jayanta Modak 16-May-22 7:53am
thanks sir

1 solution

By default, the DataType for the new column is string.
Specify the correct data-type for the columns when you add them:
Dim dt As New DataTable
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Full Name", GetType(String))
dt.Columns.Add("Parent", GetType(String))
dt.Columns.Add("Exists", GetType(Boolean))
dt.Columns.Add("Root", GetType(String))
dt.Columns.Add("Extension", GetType(String))
dt.Columns.Add("Creation Time", GetType(DateTime))
dt.Columns.Add("Creation Time Utc", GetType(DateTime))
dt.Columns.Add("LastAccess Time", GetType(DateTime))
dt.Columns.Add("LastAccess Time Utc", GetType(DateTime))
dt.Columns.Add("Last Write Time", GetType(DateTime))
dt.Columns.Add("Last WriteTime Utc", GetType(DateTime))
dt.Columns.Add("Attributes", GetType(System.IO.FileAttributes))
dt.Columns.Add("Length", GetType(Long))
dt.Columns.Add("IsReadOnly", GetType(Boolean))
Your format string should then work as expected.
Jayanta Modak 16-May-22 6:52am
thanks sir for replay
I replace my code, but how to fix it by date time format "dd/MMM/yyyy hh:mm:ss"
it is used separately or use "dt.Columns.Add("Creation Time", GetType(DateTime))"
Richard Deeming 16-May-22 9:01am
Fix the data type of the column, and the DefaultCellStyle.Format should work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900