2013-07-01 3 views
0

내 datagridview 열에는 영숫자 입력 만 허용하려고합니다. 영숫자 입력을 허용하고 사용자가 음수를 입력하거나 셀을 비워 둘 수 없게하는 방법이 있습니까? 누구든지 제안이나 답변을 가지고 있다면 크게 감사하겠습니다! :) 내 코드는 다음과 같습니다. 이미 음수 및 빈 셀 검증이 작동하지만 비 숫자 입력을 허용하지 않습니다.DataGridview에서 영숫자 텍스트를 허용하지만 음수는 제외하는 방법

If (e.ColumnIndex = 0) Then 'checking value for column 1 only 
     Dim cellData = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value 
     If cellData Is Nothing OrElse IsDBNull(cellData) OrElse cellData.ToString = String.Empty Then ' This will prevent any blank cells 
      MessageBox.Show("Name Cannot Be Empty") 
      DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = "Object" ' This is a default value that I want to supply after the message box 
     ElseIf cellData < 0 Then 
      MessageBox.Show("Negative Numbers Not Allowed") 'This prevents negative numbers 
      DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = "Object" 
      Exit Sub ' Again this a default value I want supplied back to the datagridivewcell 



     End If 
    End If 

아무런 숫자가 아닌 문자를 입력 할 때를 제외하고 내 코드가 작동하므로 프로그램이 중지되고 종료됩니다. 이 같은 TryParse를 사용

+0

아래 내 해결책을 시도해보십시오. 내가 제안한 TryParse를 제외하고 코드를 약간 수정했다. – Edper

답변

1

시도 :

If (e.ColumnIndex = 0) Then 'checking value for column 1 only 
     Dim cellValue As Integer 
     If (Int32.TryParse(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value, cellValue)) Then 
      If cellValue < 0 Then 
       MessageBox.Show("Negative Numbers Not Allowed") 'This prevents negative numbers 
       DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = "Object" 
       Exit Sub ' Again this a default value I want supplied back to the datagridivewcell 
      End If 
     Else 
      Dim testValue As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value 
      If (String.IsNullOrEmpty(testValue)) Then 
       MessageBox.Show("Name Cannot Be Empty") 
      DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = "Object" ' This is a default value that I want to supply after the message box 
      End If 
     End If 

    End If 
+0

답변 해 주셔서 감사합니다. 이것을 구현할 때 나는 여전히 편지를 입력 할 수 없습니다. – stackexchange12

+0

영숫자 입력이 가능하도록 내 코드가 업데이트되었으므로 친절하게 확인하십시오. 그러나 AlphaNumeric 항목을 허용한다는 의미가 아닌 경우 숫자가 아닌 항목도 입력 할 수 있다는 의문이 있습니다. 예를 들어 "ABC"라고 입력하면 허용됩니까? – Edper

+0

정말 대단해! 정말 고마워! – stackexchange12

관련 문제