如何仅在vb.net中的Datagridview Cell中允许一个句点

时间:2013-10-11 05:28:17

标签: vb.net datagridview cell keypress

我正在尝试使我的datagridview单元格仅接受数字和单个句点。

到目前为止,我已经成功地接受了数字,这里是代码:

    Select Case e.KeyChar
        Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", vbBack
            e.Handled = False
        Case Else
            e.Handled = True
    End Select

在我的文本框中,我也会接受数字和单个句点,这里是代码:

    Select Case e.KeyChar
        Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", vbBack
            e.Handled = False
        Case Else
            e.Handled = True
    End Select


    If (txt1.Text.IndexOf(".") >= 0 And e.KeyChar = ".") Then e.Handled = True

所有代码都在KeyPress事件中。 我不知道如何让我的datagridview单元格只接受单个句点。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

事件更好地处理你想要的是CellValueChanged:它只会检查确定的值,并最终纠正它。您也可以依靠IsNumeric快速查找有效数字。 DataGridView1的示例代码:

Private Sub DataGridView1_CellValueChanged(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged

    If (e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0) Then

        Dim curVal As String = DataGridView1(e.ColumnIndex, e.RowIndex).Value.ToString()
        If (curVal.Trim().Length > 0) Then

            If (curVal.Contains(".")) Then
                'Checking whether the given entry has more tha one period

                Dim temp() As String = curVal.Split("."c)
                If (temp.Length > 2) Then
                  'More than one period
                   DataGridView1(e.ColumnIndex, e.RowIndex).Value = temp(0) & "." & temp(1)
                ElseIf (Not IsNumeric(curVal)) Then
                   'Is not numeric
                   DataGridView1(e.ColumnIndex, e.RowIndex).Value = ""
                End If

            ElseIf (Not IsNumeric(curVal)) Then
                'Any other non-numeric entry
                DataGridView1(e.ColumnIndex, e.RowIndex).Value = ""
            End If

       End If

    End If

End Sub

请记住IsNumeric会捕获任何非数字情况(例如:1.32.52),因此我已经包含了一个前置条件来检查具有多个时间段的特定情况,以向您展示可以处理不同的情况(您可以分析整个字符串以删除特定部分,而只是删除整个单元格。)

答案 1 :(得分:0)

在DATAGRID视图中非常简单地限制重复周期按钮你只需要在EditingControlShowingEventArgs事件中提供下面提到的代码并为CONTROL_PRESS事件创建一个私有子。

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

    If TypeOf e.Control Is TextBox Then
        Dim tb As TextBox = TryCast(e.Control, TextBox)

        RemoveHandler tb.KeyPress, AddressOf CONTROL_KEYPRESS
        If Me.DataGridView1.CurrentCell.ColumnIndex = 3 Or Me.DataGridView1.CurrentCell.ColumnIndex = 4 Then
            AddHandler tb.KeyPress, AddressOf CONTROL_KEYPRESS
        End If

    End If

End Sub

Private Sub CONTROL_KEYPRESS(ByVal SENDER As TextBox, ByVal E As KeyPressEventArgs)

    If (Not Char.IsControl(E.KeyChar) And Not Char.IsDigit(E.KeyChar) And E.KeyChar <> "."c) Then
        E.Handled = True
    End If
    If (E.KeyChar = "."c And SENDER.Text.IndexOf("."c) > -1) Then
        E.Handled = True

    End If

End Sub

FURQAN HALEEM预备