[分享]DataGrid
在DataGrid中有一字段"单价"
如何写代码验证所输入值的有效性呢?
添加自定义的DataGridTextBoxColumn,重写Datagrid中的TextBox控制的KeyPress事件
Dim Ts As New DataGridTableStyle, Tc as New DataGridTextBoxColumn
AddHandler Tc.TextBox.KeyPress, New KeyPressEventHandler(AddressOf TextBoxKeyPress)
Ts.GridColumnStyles.Add(aColumnTextColumn)
DataGrid1.TableStyles.Add(Ts)
Private Sub TextBoxKeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim KeyAscII As Integer
KeyAscII = Asc(e.KeyChar)
Select Case KeyAscII
Case 48 To 57, 8, 13 '0-9,退格,回车
Case Else
KeyAscII = 0
End Select
If KeyAscII = 0 Then
e.Handled = True
Else
e.Handled = False
End If
End Sub