Datagridview Dataerror未捕获datetime列中的用户输入

时间:2015-09-22 16:57:09

标签: vb.net datagridview

我在数据类datagridview中有一个数据类型为datetime的列,它只显示时间。如果用户输入时间,则单元格的日期和时间将在.CellValueChanged事件中设置,日期来自另一列。 如果用户以正确的格式输入时间,它将按预期工作。

因此,如果用户输入“8:00”,则会将其正确解析为时间。更新日期和时间,单元格显示“8:00”

如果用户输入8,则dataerror事件将触发。

我想获取用户输入,并将其转换为时间。但是,在下面的示例中,变量numberinputNULL,而不是用户输入的内容。

我可以捕获用户输入的内容吗?

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

1 个答案:

答案 0 :(得分:0)

我想我正在吠叫错误的树,看着.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

从这个问题中直接解除了: How to restrict user input to a couple values in a DataGridView

相关问题