使用backcolor在当前行的datagridview单元格中与vb net中前一行的单元格进行比较

时间:2017-07-04 20:23:12

标签: vb.net winforms loops datagridview

我正在制定顺序罐装计划。

我使用datagridview。在以下代码的部分(2)中,每行代表一个序列和每个行单元(从该行中的第四行开始)特定罐中的液体体积。如果通过比较当前行单元格与上一行的单元格来增加体积,我希望单元格背面颜色变为绿色,如果它减小则变为黄色,如果没有变化,则颜色保持白色。 / p>

单击TkFil按钮时,单元格将从计算结果中接收数据。

我面临的问题是该行的前四个或八个单元格以随机顺序获得颜色,而其余所有其他单元格都很好。我根本无法理解并需要一些帮助:为什么着色不起作用?

这是我的完整代码:

Private Sub btnTkFil_Click(sender As System.Object, e As System.EventArgs) Handles btnTkFil.Click
    If Form36 Is Nothing Then
        MessageBox.Show("Please first open the Ballast Plan form")
    Else
        'PART(1)
        Form36.dgv1.CurrentRow.Cells(0).Value = Form1.txtCondition.Text.ToString
        Form36.dgv1.CurrentRow.Cells(1).Value = Val(Form1.txtAftDft.Text)
        Form36.dgv1.CurrentRow.Cells(2).Value = Val(Form1.txtForeDft.Text)
        Form36.dgv1.CurrentRow.Cells(3).Value = Val(tbGm1.Text)
        Try
            For i As Integer = 4 To CInt(Val(Form32.tbNbBox.Text) + 3)
                'For j As Integer = 0 To CInt(Val(Form32.tbNbBox.Text) - 1)
                For j As Integer = 0 To Form31.DataGridView1.Rows.Count - 1
                    For n As Integer = 0 To CInt(Val(Form32.tbNbBox.Text) - 1) Step +1
                        Form36.dgv1.Columns(i).Name = CStr(Form31.DataGridView1.Rows(j).Cells(0).Value)         ' noms blst tks
                        Form36.dgv1.Rows(0).Cells(i).Value = CStr(Form31.DataGridView1.Rows(j).Cells(1).Value)  'tank capa
                        If Form36.rBtn1.Checked = True And Form36.rBtn2.Checked = False Then
                            Colb(n).Text = CStr(Form31.DataGridView1.Rows(j).Cells(3).Value)                          'passe aux boxes "t" les valeurs seules reçues dans Form31.dgv et retranscrites dans GBox1.textbox en %
                            Form36.dgv1.CurrentRow.Cells(i).Value = Format(Val(Colb(n).Text), "0") & "%"
                        End If
                        If Form36.rBtn1.Checked = False And Form36.rBtn2.Checked = True Then
                            Colb(n).Text = CStr(CDbl(Form31.DataGridView1.Rows(j).Cells(1).Value) * CDbl(Form31.DataGridView1.Rows(j).Cells(3).Value) / 100)
                            Form36.dgv1.CurrentRow.Cells(i).Value = Format(Val(Colb(n).Text), "0")
                        End If
                        i += 1
                        j += 1
                    Next
                Next
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        'PART(2)
        Try
            For i As Integer = 4 To CInt(Val(Form32.tbNbBox.Text) + 3)
                For j As Integer = 2 To Form36.dgv1.Rows.Count - 2
                    Dim a As String = CStr(Form36.dgv1.Rows(j).Cells(i).Value)
                    Dim b As String = CStr(Form36.dgv1.Rows(j - 1).Cells(i).Value)
                    If a > b Then
                        Form36.dgv1.Rows(j).Cells(i).Style.BackColor = Color.PaleGreen
                    ElseIf a < b Then
                        Form36.dgv1.Rows(j).Cells(i).Style.BackColor = Color.Yellow
                    ElseIf a = b Then
                        Form36.dgv1.Rows(j).Cells(i).Style.BackColor = Color.White
                    End If
                Next
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

您正在使用字符串来比较应该是什么数字。这可能导致意想不到的结果,例如8&lt; 10但是&#34; 8&#34; &GT; &#34; 10&#34;

您只需将PART(2)中的代码修改为

即可
Dim a As Double = CDbl(Form36.dgv1.Rows(j).Cells(i).Value)
Dim b As Double = CDbl(Form36.dgv1.Rows(j - 1).Cells(i).Value)