Vb.net 如何将6位数字更改为日期字符串

Vb.net 如何将6位数字更改为日期字符串,vb.net,date,datagridview,format,Vb.net,Date,Datagridview,Format,因此,我找到了一个线程,它准确地讨论了我的问题,但是解决方案对我不起作用: 我有一个不同的数字模式,其中我的数据是:YYMMDD,只有6个数字(000901-2000年9月1日) 将数据结构转换为数据字符串时,数据结构是否会有所不同?它是否需要初始格式 这是我当前的代码字符串: DataGridView1.Columns(1).DefaultCellStyle.Format=“yy/MM/dd” 我只是想得到任何格式,但它只保留为6位数。经过一些思考和实验,我意识到如何在GUI上实现这一点,而不

因此,我找到了一个线程,它准确地讨论了我的问题,但是解决方案对我不起作用:

我有一个不同的数字模式,其中我的数据是:YYMMDD,只有6个数字(000901-2000年9月1日)

将数据结构转换为数据字符串时,数据结构是否会有所不同?它是否需要初始格式

这是我当前的代码字符串:

DataGridView1.Columns(1).DefaultCellStyle.Format=“yy/MM/dd”


我只是想得到任何格式,但它只保留为6位数。

经过一些思考和实验,我意识到如何在GUI上实现这一点,而不是在加载数据时。 您可以通过处理
DataGridView
CellFormatting
事件来实现这种特定的格式设置

我使用的测试数据:

Private Property MyData As DataTable

Private Sub InitialiseData()
    MyData = New DataTable
    MyData.Columns.Add("YMDString", GetType(String))

    Dim dr As DataRow

    dr = MyData.NewRow()
    dr(0) = "000901"
    MyData.Rows.Add(dr)

    dr = MyData.NewRow()
    dr(0) = "010901"
    MyData.Rows.Add(dr)

    dr = MyData.NewRow()
    dr(0) = "020901"
    MyData.Rows.Add(dr)
End Sub
DataGridView配置

Private Sub SetupDataGridView()
    InitialiseData()
    DataGridView1.AutoGenerateColumns = False
    DataGridView1.Columns.Add("YMDString", "YMD String")
    DataGridView1.Columns.Add("YMDDateTime", "YMD DateTime")
    DataGridView1.Columns(0).DataPropertyName = "YMDString"
    DataGridView1.Columns(1).DataPropertyName = "YMDString"
    DataGridView1.DataSource = MyData

    AddHandler DataGridView1.CellFormatting, AddressOf DataGridView1OnCellFormatting
End Sub
以及完成这项艰巨工作的
CellFormatting
事件处理程序。
DateTime
解析并不是您可以使用的最佳方法,但它确实说明了您想要实现的原则

Private Sub DataGridView1OnCellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)
    Dim thisGrid As DataGridView = CType(sender, DataGridView)

    If (thisGrid IsNot Nothing AndAlso e.Value IsNot Nothing) Then
        If (thisGrid.Columns(e.ColumnIndex).Name = "YMDDateTime") Then
            Dim stringValue As String = e.Value.ToString()
            Dim year As Integer = 2000 + stringValue.Substring(0, 2)
            Dim month As Integer = stringValue.Substring(2, 2)
            Dim day As Integer = stringValue.Substring(4, 2)

            Dim dt As New DateTime(year, month, day)

            e.Value = dt.ToString("yyyy/MM/dd")
        End If
    End If
End Sub
运行时,我得到以下结果:


经过一些思考和实验,我意识到如何在GUI上而不是在加载数据时执行此操作。 您可以通过处理
DataGridView
CellFormatting
事件来实现这种特定的格式设置

我使用的测试数据:

Private Property MyData As DataTable

Private Sub InitialiseData()
    MyData = New DataTable
    MyData.Columns.Add("YMDString", GetType(String))

    Dim dr As DataRow

    dr = MyData.NewRow()
    dr(0) = "000901"
    MyData.Rows.Add(dr)

    dr = MyData.NewRow()
    dr(0) = "010901"
    MyData.Rows.Add(dr)

    dr = MyData.NewRow()
    dr(0) = "020901"
    MyData.Rows.Add(dr)
End Sub
DataGridView配置

Private Sub SetupDataGridView()
    InitialiseData()
    DataGridView1.AutoGenerateColumns = False
    DataGridView1.Columns.Add("YMDString", "YMD String")
    DataGridView1.Columns.Add("YMDDateTime", "YMD DateTime")
    DataGridView1.Columns(0).DataPropertyName = "YMDString"
    DataGridView1.Columns(1).DataPropertyName = "YMDString"
    DataGridView1.DataSource = MyData

    AddHandler DataGridView1.CellFormatting, AddressOf DataGridView1OnCellFormatting
End Sub
以及完成这项艰巨工作的
CellFormatting
事件处理程序。
DateTime
解析并不是您可以使用的最佳方法,但它确实说明了您想要实现的原则

Private Sub DataGridView1OnCellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)
    Dim thisGrid As DataGridView = CType(sender, DataGridView)

    If (thisGrid IsNot Nothing AndAlso e.Value IsNot Nothing) Then
        If (thisGrid.Columns(e.ColumnIndex).Name = "YMDDateTime") Then
            Dim stringValue As String = e.Value.ToString()
            Dim year As Integer = 2000 + stringValue.Substring(0, 2)
            Dim month As Integer = stringValue.Substring(2, 2)
            Dim day As Integer = stringValue.Substring(4, 2)

            Dim dt As New DateTime(year, month, day)

            e.Value = dt.ToString("yyyy/MM/dd")
        End If
    End If
End Sub
运行时,我得到以下结果:


我将您的六位数字符串转换为日期时间,然后按照您的意愿格式化

Dim strDate As String = "180211"
Dim myDate As New DateTime(CInt("20" & strDate.Substring(0, 2)), CInt(strDate.Substring(2, 2)), CInt(strDate.Substring(4)))
Debug.Print(myDate.ToString("yy/MM/dd"))
编辑: 我找到了一个更好的答案,多亏了位于


我把你的六位数字符串转换成日期时间,然后按照你的意愿格式化

Dim strDate As String = "180211"
Dim myDate As New DateTime(CInt("20" & strDate.Substring(0, 2)), CInt(strDate.Substring(2, 2)), CInt(strDate.Substring(4)))
Debug.Print(myDate.ToString("yy/MM/dd"))
编辑: 我找到了一个更好的答案,多亏了位于


好的,试试这个。将datagridview中的格式设置为要使用的日期格式。例如,我有3列,最右边的一列包含6位字符串

在我的第3列中,我在datagridview中设置了yyyy/MM/dd格式

在button click中,我循环遍历dgv,并将值解析为datetime,这使得格式可以像现在处理日期类型一样工作

    For Each r As DataGridViewRow In DataGridView1.Rows
        If Not r.IsNewRow Then
            r.Cells(2).Value = DateTime.ParseExact(r.Cells(2).Value.ToString, "yyMMdd", CultureInfo.InvariantCulture)
        End If
    Next
点击按钮,我得到了以下结果

既然列是日期数据类型,我们就可以将格式和列格式更改为新的格式设置

我添加了第二个按钮,并在该按钮中放置了代码

DataGridView1.Columns(2).DefaultCellStyle.Format = "MMM dd, yyyy"
这会立即将第3列更改为新格式


好的,试试这个。将datagridview中的格式设置为要使用的日期格式。例如,我有3列,最右边的一列包含6位字符串

在我的第3列中,我在datagridview中设置了yyyy/MM/dd格式

在button click中,我循环遍历dgv,并将值解析为datetime,这使得格式可以像现在处理日期类型一样工作

    For Each r As DataGridViewRow In DataGridView1.Rows
        If Not r.IsNewRow Then
            r.Cells(2).Value = DateTime.ParseExact(r.Cells(2).Value.ToString, "yyMMdd", CultureInfo.InvariantCulture)
        End If
    Next
点击按钮,我得到了以下结果

既然列是日期数据类型,我们就可以将格式和列格式更改为新的格式设置

我添加了第二个按钮,并在该按钮中放置了代码

DataGridView1.Columns(2).DefaultCellStyle.Format = "MMM dd, yyyy"
这会立即将第3列更改为新格式


数据是来自文件还是数据库?我认为在最早的时间点更改数据类型可能比在DataGridView上更改数据类型更好/更容易。对于这种特定格式,数据基本上来自.log文件(或纯文本),我使用固定宽度的分隔符来解析文件,并且提取的一组数据是日期,另一组是时间戳,我还尝试了
CDate(DataGridView1.Rows(1.Cells(1.Value)).ToString(“yyyyMMdd”)
但是我得到了一个错误,说
System.InvalidCastException:“从字符串“000901”到类型“Date”的转换无效。”
关于
dt as DateTime=DateTime.ParseExact(“000901”,“yyMMdd”怎么办,CultureInfo.InvariantCulture)
那么当您显示它时,只需在
dt.ToString(“yyyyMMdd”)中设置格式即可。
数据是否来自文件或数据库?我认为在最早的时间点更改数据类型可能比在DataGridView上更改数据类型更好/更容易。对于这种特定格式,数据基本上来自.log文件(或纯文本),我使用固定宽度的分隔符来解析文件,并且提取的一组数据是日期,另一组是时间戳,我还尝试了
CDate(DataGridView1.Rows(1.Cells(1.Value)).ToString(“yyyyMMdd”)
但是我得到了一个错误,说
System.InvalidCastException:“从字符串“000901”到类型“Date”的转换无效。”
关于
dt as DateTime=DateTime.ParseExact(“000901”,“yyMMdd”怎么办,CultureInfo.InvariantCulture)
然后,当您显示它时,只需在
dt.ToString(“yyyyMMdd”)中设置格式即可。
这非常漂亮、简单、优雅,效果非常好!我很容易就能适应这项工作的时间以及。非常感谢!!!非常感谢。这是美丽,简单和优雅,并作品的待遇!我很容易就能适应这项工作的时间以及。非常感谢!!!非常感谢。