DataGridView Numeric Only Cell?

前端 未结 8 638
广开言路
广开言路 2021-01-06 07:33

I am new to winforms..I am trying to set two column of DataGridView to Numeric Only.. I do not want user to be able to type anything into a cell unless its a natural number

8条回答
  •  甜味超标
    2021-01-06 07:57

    Following code is an extension of Satish's Solution. It will help to control values of DataGridView Cells. Textbox function has used to attach a cell to textbox-event only. No need to add a text box in a DataGridView or anywhere on the form.

    Private Sub DataGridView_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView.EditingControlShowing
        If DataGridView.CurrentCell.ColumnIndex = 2 Then 'Numeric column with decimal point
            AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
    
        ElseIf DataGridView.CurrentCell.ColumnIndex = 3 Then 'Numeric column without Decimal
            AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress1
    
        ElseIf DataGridView.CurrentCell.ColumnIndex = 4 Then 'Selected Values only
            AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress2
    
        ElseIf DataGridView.CurrentCell.ColumnIndex = 5 Then 'Email Column
            AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress3
    
        End If
    
    End Sub
    
    Private Sub TextBox_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        'Allows Numeric values, one decimal point and BackSpace key
        Dim numbers As Windows.Forms.TextBox = sender
        If InStr("1234567890.", e.KeyChar) = 0 And Asc(e.KeyChar) <> 8 Or (e.KeyChar = "." And InStr(numbers.Text, ".") > 0) Then
            e.KeyChar = Chr(0)
            e.Handled = True
        End If
    End Sub
    
    Private Sub TextBox_keyPress1(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        'Allow Numeric values, BackSpace key. Disallows decimal point (i.e. dot) 
        Dim numbers As Windows.Forms.TextBox = sender
        If InStr("1234567890", e.KeyChar) = 0 And Asc(e.KeyChar) <> 8 Then
            e.KeyChar = Chr(0)
            e.Handled = True
        End If
    End Sub
    
    
    Private Sub TextBox_keyPress2(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        'Allow selected values only
        If InStr("1234567890!@#$%^&*()_+=-", e.KeyChar) > 0 Then
            e.KeyChar = Chr(0)
            e.Handled = True
        End If
    End Sub
    
    Private Sub TextBox_keyPress3(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        'Martch function, Needs to add "Imports System.Text.RegularExpressions" at the top of Class
        'Allows Email values
        Dim Email As Windows.Forms.TextBox = sender
        If Email.Text <> "" Then
            Dim rex As Match = Regex.Match(Trim(Email.Text), "^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,3})$", RegexOptions.IgnoreCase)
            If rex.Success = False Then
                MessageBox.Show("Please Enter a valid Email Address", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Email.BackColor = Color.Red
                Email.Focus()
                Exit Sub
            Else
                Email.BackColor = Color.White
            End If
        End If
    End Sub
    

提交回复
热议问题