2013-07-03 27 views
1

,我希望能找到一種方法來進入非數字輸入到我的DataGridViewColumn限制用戶的非數字輸入。此外,我已經限制用戶輸入任何負數並將單元格留空。如果任何人都可以找到一種方法來限制用戶輸入字母和非數字輸入,我將不勝感激!如何限制在datagridview的

If (e.ColumnIndex = 8) Then 'This specifies the column number 
     Dim cellData = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value 
     If cellData Is Nothing OrElse IsDBNull(cellData) OrElse cellData.ToString = String.Empty Then 
      MessageBox.Show("Cannot Be Empty") 
      DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 0 
     ElseIf cellData < 0 Then 
      MessageBox.Show("Negatives Values Not Allowed") 
      DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 0 

      Exit Sub 

     End If 
    End If 

回答

1

試試看。如果使用DataGridView.KeyPress事件來監視什麼剛鍵入,您可以檢查字符與Char.IsDigit(e.KeyChar)什麼。

Private Sub DataGridView1_KeyPress(sender As Object, e As  System.Windows.Forms.KeyPressEventArgs) Handles DataGridView1.KeyPress 
    If (Char.IsDigit(e.KeyChar)) Then 
     'this is a number 
    Else 
     'not a number 
    End If 
End Sub 
+0

對人有好處,謝謝! – stackexchange12

+0

不客氣。 –