【问题标题】:How to add datetimepicker in datagridview in Visual Basic?如何在 Visual Basic 的 datagridview 中添加 datetimepicker?
【发布时间】:2016-04-03 16:40:06
【问题描述】:

我有一个 datagridview (DataGridViewsalarydetail)。它有 3 列:

  1. 公式(只接受数字和字母),
  2. 生效日期(仅接受日期:14/09/2016),
  3. 基本费率(仅接受数字和点 [.] 表示双精度)。

问题是如何将日期时间选择器放在数据网格视图中或如何将日期列设置为只接受有效日期?

我正在使用 Visual Basic Express 2015、MySQL、phpMyAdmin 和 VB.NET

我尝试过 MSDN 网站上的代码,但我不知道如何在我的项目中实现。

Private Sub DataGridViewsalarydetail_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridViewsalarydetail.EditingControlShowing
        If TypeOf e.Control Is TextBox Then
            DirectCast(e.Control, TextBox).CharacterCasing = CharacterCasing.Upper
    End If

    If DataGridViewsalarydetail.CurrentCell.ColumnIndex = 0 Then

        AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress

    ElseIf DataGridViewsalarydetail.CurrentCell.ColumnIndex = 1 Then

        AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress2

    ElseIf DataGridViewsalarydetail.CurrentCell.ColumnIndex = 2 Then

        AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress1

    End If

End Sub

Private Sub TextBox_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)

    'If Char.IsDigit(CChar(CStr(e.KeyChar))) = False Then e.Handled = True

End Sub

Private Sub TextBox_keyPress1(ByVal sender As Object, ByVal e As KeyPressEventArgs)

    'If Not (Char.IsDigit(CChar(CStr(e.KeyChar))) Or e.KeyChar = ".") Then e.Handled = True
    e.Handled = True
    If e.KeyChar Like "." Or e.KeyChar = Chr(&H8) Or IsNumeric(e.KeyChar) Then
        e.Handled = False
    End If

End Sub
Private Sub TextBox_keyPress2(ByVal sender As Object, ByVal e As KeyPressEventArgs)

    If Not (Char.IsDigit(CChar(CStr(e.KeyChar))) Or e.KeyChar = "/") Then e.Handled = True
    'e.Handled = True
    'If e.KeyChar Like "." Or e.KeyChar Like "/" Or e.KeyChar = Chr(&H8) Or IsNumeric(e.KeyChar) Then
    '    e.Handled = False
    'End If

End Sub

Private Sub DataGridViewsalarydetail_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridViewsalarydetail.CellValidating
    If e.ColumnIndex = 1 Then
        Dim dt As DateTime
        If e.FormattedValue.ToString <> String.Empty AndAlso Not DateTime.TryParse(e.FormattedValue.ToString, dt) Then
            MessageBox.Show("Enter correct Date")
            'Me.DataGridViewsalarydetail.Rows(e.RowIndex).ErrorText = "Enter a valid Date time"
            e.Cancel = True
        End If
    End If
End Sub

【问题讨论】:

    标签: vb.net datagridview


    【解决方案1】:

    尝试使用datagridview.CellEndEdit event检查用户输入是否正确。

    您可以像我在下面的示例中那样使用IsDate function,或者您可以创建自己的函数来验证用户输入。

    Public Sub effectiveDate() Handles DataGridViewsalarydetail.CellEndEdit
        'Get current RowIndex and ColumnIndex
        Dim row As Integer = DataGridViewsalarydetail.CurrentCell.RowIndex
        Dim column As Integer = DataGridViewsalarydetail.CurrentCell.ColumnIndex()
    
        If column = 1 Then 'If you are currently in the second column of the datagridview
            If IsDate(DataGridViewsalarydetail.Rows(row).Cells(column).Value) = False Then
                MsgBox("The date of the " & row & " row is not correct")
                Dim cell As Windows.Forms.DataGridViewCell = DataGridViewsalarydetail.Rows(row).Cells(column)
                'Focus the cell 
                DataGridViewsalarydetail.CurrentCell = cell
                DataGridViewsalarydetail.BeginEdit(True)
            End If
        End If
    End Sub
    

    【讨论】:

    • 我试过那个代码。但它给了我一个错误。你能给我一个例子,我可以在哪里实现代码?
    • 编辑你的问题并向我展示你的datagridview的一些代码
    • 对不起先生,我是 vb 代码的新手。那么,我应该添加什么来运行您的代码?还有一件事,我可以在我的datagridview中放置日期时间选择器而不创建新的datagridview吗?我的意思是只需放入现有的 clolumn。
    • 如果您真的需要在 datagridview 中使用 datetimepicker,请查看 MSDN,这里有一些文档。您也可以查看Code Projet,有一些源文件可能对您有所帮助。
    • 好的,先生。非常感谢您 。现在我准备展示我的项目。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 2022-11-12
    • 2014-07-06
    相关资源
    最近更新 更多