我需要验证datagridview中特定单元格中的值是否在某个范围内(0 - 99.9999),如果不是,则取消用户编辑。到目前为止我所做的是验证一个列是双数据类型的列,它是>这有效。但现在我需要验证该列中的每个特定单元格,因为它们都需要针对特定数值范围进行验证。每个单元格都可以接受不同的值范围。
Private Sub dgvPidVals0_cellValidating(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles dgvPidVals0.CellValidating
'Me.dgvPIDStatus0.Rows(e.ColumnIndex).ErrorText = ""
Dim newDouble As Double
Select Case e.ColumnIndex
Case 1
If Not Double.TryParse(e.FormattedValue.ToString(), newDouble) OrElse newDouble < 0 Then
e.Cancel = True
MessageBox.Show("PID Parameters values must be non-negative numeric values only!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
dgvPidVals0.CancelEdit()
If dgvPidVals0.Rows(5).Cells(1).Value > 99.9999 Then
e.Cancel = True
MessageBox.Show("Output Filter Value must be less than 100.0!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
dgvPidVals0.CancelEdit()
End If
End If
End Select
End Sub
答案 0 :(得分:0)
Tez,我已经尝试过那些确切的代码而且它不起作用。我不知道为什么不这样做。代码的第一个If语句工作正常。否则If部分不起作用。我在DGV的单元格中输入一个大于100的值,它需要(当它不应该)。然后我再次单击该表单,然后出现消息框。由于每次点击DGV都会显示消息框,因此我无法执行任何操作。最后,我使用CellEndEdit事件处理程序检查我评估的每个单元格中的值是否在一定范围内。我认为这是一种被黑客攻击的方式,我认为有更好,更干净的方式。但是......它有效。仍然可以接受其他人的答案,了解该怎么做或者使用CellEndEdit事件是实际的方法。
Private Sub dgvPidVals0_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dgvPidVals0.CellEndEdit
If dgvPidVals0.Rows(0).Cells(1).Value > 6.14 Then
dgvPidVals0.Rows(0).Cells(1).Value = axis0Parameters(61) 'Load previous value into the cell
MessageBox.Show("Proportional Gain Value must be <= 6.14!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
If dgvPidVals0.Rows(1).Cells(1).Value > 58.94 Then
dgvPidVals0.Rows(1).Cells(1).Value = axis0Parameters(62)
MessageBox.Show("Integral Gain Value must be <= 58.94!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
If dgvPidVals0.Rows(2).Cells(1).Value > 0.1753 Then
dgvPidVals0.Rows(2).Cells(1).Value = axis0Parameters(63)
MessageBox.Show("Differential Gain Value must be <= 0.1753!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
If dgvPidVals0.Rows(3).Cells(1).Value > 5 Then
dgvPidVals0.Rows(3).Cells(1).Value = axis0Parameters(64)
MessageBox.Show("Feed Forward Value must be <= 5!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
If dgvPidVals0.Rows(4).Cells(1).Value > 1.5 Then
dgvPidVals0.Rows(4).Cells(1).Value = axis0Parameters(65)
MessageBox.Show("Rate Feed Forward Value must be <= 1.5!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
If dgvPidVals0.Rows(5).Cells(1).Value > 100 Then
dgvPidVals0.Rows(5).Cells(1).Value = axis0Parameters(39)
MessageBox.Show("Output Filter Value must be <= 100.0!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
slider1.Value = dgvPidVals0.Item(1, 0).Value
slider2.Value = dgvPidVals0.Item(1, 1).Value
slider3.Value = dgvPidVals0.Item(1, 2).Value
slider4.Value = dgvPidVals0.Item(1, 3).Value
slider5.Value = dgvPidVals0.Item(1, 4).Value
slider6.Value = dgvPidVals0.Item(1, 5).Value
End Sub