我有一个datagridview列,我希望阻止用户将单元格留空或输入负数。我发现当我更改if then语句的顺序以进行空白验证时,首先检查代码是否正常,但不是负面验证,反之亦然。那么为什么代码只适用于第一个if语句并忽略第二个?我非常感谢任何人可以给予的任何帮助或建议。 :)
If (e.ColumnIndex = 0) Then 'checking value for column 1 only
Dim cellData As Integer
If (Int32.TryParse(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value, cellData)) Then
If cellData < 0 Then
MessageBox.Show("Negative Numbers Not Allowed") 'This prevents negative numbers
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = "Name"
Exit Sub ' Again this a default value I want supplied back to the datagridivewcell
End If
Else
Dim testData As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
If (String.IsNullOrEmpty(testData)) Then
MessageBox.Show("Cannot Be Empty")
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = "Name" ' This is a default value that I want to supply after the message box
End If
End If
End If
答案 0 :(得分:1)
If (e.ColumnIndex = 0) Then 'checking value for column 1 only
Dim cellData As Integer
If Not DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value Is DBNull.Value Then
If (Int32.TryParse(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value, cellData)) Then
If cellData < 0 Then
MessageBox.Show("Negative Numbers Not Allowed")
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = "Vendor Name"
Exit Sub
End If
End If
If (String.IsNullOrEmpty(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)) Then
MessageBox.Show("Cannot Be Empty")
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = "Vendor Name"
Exit Sub '
End If
Else
MessageBox.Show("Cannot Be Empty")
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = "Vendor Name"
Exit Sub
End If
End If