验证检查仅检查第一个if语句并忽略第二个 [英] Validation check is only checking the first if statement and ignoring the second

查看:122
本文介绍了验证检查仅检查第一个if语句并忽略第二个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview列,我希望阻止用户将单元格留空或输入负数。我发现当我更改if then语句的顺序以进行空白验证时,首先检查代码是否正常,但不是负面验证,反之亦然。那么为什么代码只适用于第一个if语句并忽略第二个?我非常感谢任何人可以给予的任何帮助或建议。 :)



 如果(e.ColumnIndex =  0 然后 ' 仅检查第1列的值 
Dim cellData As 整数

如果 Int32 .TryParse(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value,cellData))然后
如果 cellData< 0 然后
MessageBox.Show( 不允许使用负数' 这可以防止负数
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 名称
退出 Sub ' 这又是一个我希望提供给datagridivewcell的默认值
End 如果
其他
Dim testData As String = DataGridVi ew1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
If String .IsNullOrEmpty(testData))然后
MessageBox.Show( 不能为空
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 名称 ' 这是我的默认值想要在消息框后面提供
结束 如果
< span class =code-keyword>结束 如果

结束 如果

解决方案

如果必须同时检查单元格缺点ditions尝试以下代码:



 如果(e.ColumnIndex =  0 然后 ' 仅检查第1列的值 
Dim cellData As 整数

IF DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value DBNull.Value Then
如果 Int32 .TryParse(DataGridView1.Rows(e.RowIndex)) .Cells(e.ColumnIndex).Value,cellData))然后
如果 cellData< 0 然后
MessageBox.Show( 不允许使用负数
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 名称
退出 Sub
结束 如果
结束 如果
如果 String .IsNullOrEmpty(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value))然后
MessageBox.Show( 不能为空
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 名称
退出 Sub '
结束 如果
其他
MessageBox.Show( 不能是清空
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 名称
退出 Sub
结束 如果
结束 如果


I have a datagridview column that I wish to prevent the user from leaving the cell blank or input negative numbers. I've found that when I change the order of the if then statements to have the blank validation check first the code works, but not for the negative validation and vice-versa. So why is it that the code is only working for the first if statement and ignoring the second? I greatly appreciate any help or suggestions anyone can give on this. :)

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

解决方案

If the cell has to be checked for both the conditions try the following code:

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 = "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 = "Name"
                Exit Sub '
            End If
        Else
            MessageBox.Show("Cannot Be Empty")
            DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = "Name"
            Exit Sub
        End If
End If


这篇关于验证检查仅检查第一个if语句并忽略第二个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆