日付が短縮形で入力された場合、yyyyMMdd形式に変換する
VB
Private Sub DataGridView1_CellParsing(sender As Object, e As DataGridViewCellParsingEventArgs) Handles DataGridView1.CellParsing
If DataGridView1.Columns(e.ColumnIndex).Name <> "Date" Then Return
If e.Value = "" Then Return
Try
e.Value = DateTime.Parse(e.Value.ToString()).ToString("yyyy/MM/dd")
e.ParsingApplied = True
Catch ex As FormatException
e.ParsingApplied = False
End Try
End Sub
動作確認用コード
VB
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable
With dt
.Columns.Add("ProductCode", GetType(String))
.Columns.Add("Date", GetType(String))
.Rows.Add("A001", "2023/03/12")
.Rows.Add("A002", "2000/02/04")
.Rows.Add("A003", "2020/12/10")
.Rows.Add("A004", "2026/09/01")
End With
With DataGridView1
.DataSource = dt
End With
End Sub
コメント