![](/img/trans.png)
[英]DataGridView DateTime column string was not recognized as a valid DateTime DataError
[英]Datagridview Dataerror not capturing user input in datetime column
我在數據綁定的datagridview
有一列,其datagridview
類型為datetime
,僅顯示時間。 如果用戶輸入時間,則在.CellValueChanged
事件中設置單元格的日期和時間,日期來自另一列。 如果用戶以正確的格式輸入時間,它將按預期工作。
因此,如果用戶輸入“ 8:00”,則將其正確解析為時間。 日期和時間被更新,並且單元格顯示“ 8:00”
如果用戶輸入8, dataerror
觸發dataerror
事件。
我想獲取用戶輸入,並將其轉換為時間。 但是,在下面的示例中,變量numberinput
為NULL
,根本不是用戶輸入的內容。
我可以捕獲用戶輸入的內容嗎?
Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
'handle incorrect date formats
e.ThrowException = False
Dim numberinput As Single
numberinput = Val(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
Select Case True
Case Val(numberinput) = 0
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = DBNull.Value
Case numberinput = Int(numberinput)
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = RelevantDate.AddHours(numberinput)
End Select
e.Cancel = False
End Sub
我想我正在錯誤地看着.DataError樹
這有效:
Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If DataGridView1.CurrentRow.Cells(e.ColumnIndex).IsInEditMode Then
Dim c As Control = DataGridView1.EditingControl
If DataGridView1.Columns(e.ColumnIndex).ValueType = Type.GetType("System.DateTime") Then
c.Text = CleanInputDate(c.Text)
End If
End If
End Sub
從這個問題中獲得了很多直接的啟示: 如何在DataGridView中將用戶輸入限制為幾個值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.