我是新来的winforms..I我想设置DataGridView的两列数字只有..我不希望用户能够将任何东西,除非它的一个细胞在一列中键入一个自然数和数值在另一个(其中始终保留一位小数)。 我认为这将是简单的..但即使尝试了很多来自计算器和其他网站的东西我仍然无法实现这一目标。
如果DataGridView1.CurrentCell.ColumnIndex = 8然后
如果不Char.IsControl(e.KeyChar)AndAlso不Char.IsDigit(e.KeyChar)AndAlso e.KeyChar<> C,则
e.Handled = TRUE
结束如果
结束如果
解决方案
试试这个code
私人小组DataGridView1_EditingControlShowing(BYVAL发件人为对象,BYVAL E上System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)处理DataGridView1.EditingControlShowing
如果DataGridView1.CurrentCell.ColumnIndex = 2那么
AddHandler的CTYPE(e.Control,文本框)。重点preSS,AddressOf TextBox_key preSS
elseif的DataGridView1.CurrentCell.ColumnIndex = 1,则
AddHandler的CTYPE(e.Control,文本框)。重点preSS,AddressOf TextBox_key preSS1
结束如果
结束小组
私人小组TextBox_key preSS(BYVAL发件人为对象,BYVALË作为重点pressEventArgs)
如果Char.IsDigit(CCHAR(CSTR(e.KeyChar)))= false,那么e.Handled = TRUE
结束小组
私人小组TextBox_key preSS1(BYVAL发件人为对象,BYVALË作为重点pressEventArgs)
如果不是(Char.IsDigit(CCHAR(CSTR(e.KeyChar)))或者e.KeyChar =。)然后e.Handled =真
结束小组
TextBox_key preSS事件只有数字
TextBox_key preSS1事件的数字与十进制值
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 in one column and a numeric value in another(which always one decimal). I thought this would be simple.. but even after trying a lot of things from stackoverflow and other sites i am still unable to achieve this.
If DataGridView1.CurrentCell.ColumnIndex = 8 Then
If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> "."c Then
e.Handled = True
End If
End If
解决方案
Try this code
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCell.ColumnIndex = 2 Then
AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
ElseIf DataGridView1.CurrentCell.ColumnIndex = 1 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
End Sub
TextBox_keyPress Event for only numeric
TextBox_keyPress1 Event for numeric with decimal value