DataGirdView 单元格限制内容输入参考(按键时的判断)

限制DataGridView的输入

DataGridView
 1     '******************************************************************************************
2 ''' <summary>AddHandler DGV key press</summary>
3 Private Sub DGV_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvMain.EditingControlShowing
4 Try
5 Dim CurEditrCell As DataGridViewTextBoxEditingControl
6
7 If TypeOf e.Control Is DataGridViewTextBoxEditingControl Then
8 CurEditrCell = CType(e.Control, DataGridViewTextBoxEditingControl)
9 AddHandler CurEditrCell.KeyPress, AddressOf dgvKeyPress
10 End If
11 Catch ex As Exception
12
13 End Try
14 End Sub
15
16 '******************************************************************************************
17 ''' <summary>DGV Cell key press event</summary>
18 Private Sub dgvKeyPress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs)
19 Try
20 With dgvMain
21 If .CurrentCell Is Nothing Then
22 Exit Sub
23 End If
24
25 If (Asc(e.KeyChar) <> Keys.Back) Then
26 Select Case .CurrentCell.ColumnIndex
27 Case 2, 3
28 If (sender.text.length < 10) Then
29 If (e.KeyChar <> Chr(47)) AndAlso ((e.KeyChar > Chr(57)) OrElse (e.KeyChar < Chr(48))) Then
30 e.Handled = True
31 End If
32 Else
33 e.Handled = True
34 End If
35 End Select
36 End If
37
38 End With
39 Catch ex As Exception
40
41 End Try
42 End Sub



原文地址:https://www.cnblogs.com/sugartomato/p/2303860.html