首先,以下是我的代码:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Establish connection with sql server. Fill the data in data grid view (dgvData)
For i As Integer = 2 To dgvData.Columns.Count() - 1
dgvSettings.Rows.Add(dgvData.Columns(i).HeaderText, 0, 100, "High")
Next
End Sub
Public Sub comparevalues()
For z As Integer = 2 To dgvData.ColumnCount - 1
If dgvData.Rows(0).Cells(z).Value.ToString() > dgvSettings.Rows(z - 2).Cells(2).Value.ToString() Then
MsgBox("Value Exceed Maximum Limit")
dgvAlert.Rows.Add(False, dgvData.Rows(0).Cells(1).Value.ToString(), dgvData.Columns(z).HeaderText.ToString(), dgvData.Rows(0).Cells(z).Value.ToString(), "Unacknowledged", dgvSettings.Rows(z - 2).Cells(3).Value.ToString())
ElseIf dgvData.Rows(0).Cells(z).Value.ToString() < dgvSettings.Rows(z - 2).Cells(1).Value.ToString() Then
MsgBox("Value Below Minimum Limit")
dgvAlert.Rows.Add(False, dgvData.Rows(0).Cells(1).Value.ToString(), dgvData.Columns(z).HeaderText.ToString(), dgvData.Rows(0).Cells(z).Value.ToString(), "Unacknowledged", dgvSettings.Rows(z - 2).Cells(3).Value.ToString())
Else
'in range, do nothing
End If
Next
End Sub
this is dgvdata table, it is linked to my sql server db
this is dgvsettings table, the limit conditions are all set in here.
所以这是我正在尝试做的事情,尽管其中一些人可能能够从上面给出的代码中进行解释。用我的sql数据表填充dgvData后,我想根据comparevalues()子集中设置的条件比较最新行的每一列中的值。即。如果输入的值(例如列pH)超过了dgvSettings中设置的值限制,那么它将添加到dgvAlert表中。
但是问题是,当我运行代码时,那些在dgvSettings中设置的范围内的代码仍会添加到dgvAlert中。当它们仍应在范围限制内时,它们中的大多数被视为超出限制。从过去的几天开始,我一直在努力解决问题。非常感谢您的帮助。谢谢。
答案 0 :(得分:1)
您写错了。
如果要比较它们,必须首先将这些字符串转换为实际数字。
Public Sub comparevalues()
For z= 2 To dgvData.ColumnCount - 1
Dim Cell1 as Double = CDbl(dgvData(z,0).Value.ToString)
Dim Cell2 as Double = CDbl(dgvSettings(2,z-2).Value.ToString)
Dim Cell3 as Double = CDbl(dgvSettings(1,z-2).Value.ToString)
If Cell1 > Cell2 Then
MsgBox("Value Exceed Maximum Limit")
dgvAlert.Rows.Add(False, dgvData(1,0).Value.ToString, dgvData.Columns(z).HeaderText.ToString(), dgvData(z,0).Value.ToString, "Unacknowledged", dgvSettings(3,z-2).Value.ToString)
ElseIf Cell1 < Cell3 Then
MsgBox("Value Below Minimum Limit")
dgvAlert.Rows.Add(False, dgvData(1,0).Value.ToString, dgvData.Columns(z).HeaderText.ToString(), dgvData(z,0).Value.ToString, "Unacknowledged", dgvSettings(3,z-2).Value.ToString)
Else
'in range, do nothing
End If
Next
End Sub
答案 1 :(得分:0)
请尝试以下操作,以了解为什么将带有<和>的字符串进行比较不适用于您的情况。
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim x As String = "7".ToString
Dim y As String = "10".ToString
If x > y Then
MessageBox.Show("7 is greater than 10")
ElseIf x < y Then
MessageBox.Show("10 is greater than 7")
End If
End Sub
摆脱.ToString