尝试将日期和时间数据从 DataGridView 播种到 DateTimePicker 时,字符串未被识别为有效的 DateTime 错误
String was not recognized as a valid DateTime Error when try to sow date and time data into DateTimePicker from DataGridView
我想显示日期和时间 (格式:dd/MM/yyyy h:mm tt) 从“DateTime 到 DateTimePicker”列。但它显示错误 字符串未被识别为有效的日期时间。
Private Sub dgvSchedule_RowEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgvSchedule.RowEnter
'DISPLAYS COLUMN VALUE IN TEXTBOXES
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.dgvSchedule.Rows(e.RowIndex)
txtID.Text = row.Cells("Patient_ID_Number").Value.ToString
dtpDate.Text = row.Cells("Date_Time").Value 'THIS IS WHERE THE ERROR OCCUR
txtFirstname.Text = row.Cells("Firstname").Value.ToString
txtLastname.Text = row.Cells("Lastname").Value.ToString
txtPhoneNumber.Text = row.Cells("Phone_Number").Value.ToString
End If
End Sub
您的 'Date_Time' 列的格式是什么?
如果它是这样的 MySQL 日期时间:2016-01-23 23:30:55
那么您可以使用 VB.NET
的 DateTime.Parse
Dim new_date As DateTime = DateTime.Parse("2016-01-23 23:30:55")
DateTimePicker1.Value = new_date
注意格式是'Custom',自定义格式是dd/MM/yyyy h:mm tt
DateTimePicker1.Format = DateTimePickerFormat.Custom
DateTimePicker1.CustomFormat = "dd/MM/yyyy h:mm tt"
所以最终代码可能是:
DateTimePicker1.Format = DateTimePickerFormat.Custom
DateTimePicker1.CustomFormat = "dd/MM/yyyy h:mm tt"
Dim new_date As DateTime = DateTime.Parse(row.Cells("Date_Time").Value.ToString())
DateTimePicker1.Value = new_date
我还注意到您正在使用:
dtpDate.Text = row.Cells("Date_Time").Value
而不是
dtpDate.Value = row.Cells("Date_Time").Value
请注意,它们是不同的:Text 和 Value。
我想显示日期和时间 (格式:dd/MM/yyyy h:mm tt) 从“DateTime 到 DateTimePicker”列。但它显示错误 字符串未被识别为有效的日期时间。
Private Sub dgvSchedule_RowEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgvSchedule.RowEnter
'DISPLAYS COLUMN VALUE IN TEXTBOXES
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.dgvSchedule.Rows(e.RowIndex)
txtID.Text = row.Cells("Patient_ID_Number").Value.ToString
dtpDate.Text = row.Cells("Date_Time").Value 'THIS IS WHERE THE ERROR OCCUR
txtFirstname.Text = row.Cells("Firstname").Value.ToString
txtLastname.Text = row.Cells("Lastname").Value.ToString
txtPhoneNumber.Text = row.Cells("Phone_Number").Value.ToString
End If
End Sub
您的 'Date_Time' 列的格式是什么?
如果它是这样的 MySQL 日期时间:2016-01-23 23:30:55
那么您可以使用 VB.NET
DateTime.Parse
Dim new_date As DateTime = DateTime.Parse("2016-01-23 23:30:55")
DateTimePicker1.Value = new_date
注意格式是'Custom',自定义格式是dd/MM/yyyy h:mm tt
DateTimePicker1.Format = DateTimePickerFormat.Custom
DateTimePicker1.CustomFormat = "dd/MM/yyyy h:mm tt"
所以最终代码可能是:
DateTimePicker1.Format = DateTimePickerFormat.Custom
DateTimePicker1.CustomFormat = "dd/MM/yyyy h:mm tt"
Dim new_date As DateTime = DateTime.Parse(row.Cells("Date_Time").Value.ToString())
DateTimePicker1.Value = new_date
我还注意到您正在使用:
dtpDate.Text = row.Cells("Date_Time").Value
而不是
dtpDate.Value = row.Cells("Date_Time").Value
请注意,它们是不同的:Text 和 Value。