更新时间:2023-02-06 19:19:50
如果必须同时检查单元格缺点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 IfIf 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