0
votes

I am working out a sequential tank filling plan.

I use a datagridview. In part (2) of the following code, each row represents a sequence and each row cell (beginning from the fourth one in the row) the volume of liquid in a specific tank. If it appears by comparing the current row cell to that of the previous row that the volume has increased, I want the cell backcolor become green, if it decreased then it's yellow color and if no change the color remains white.

The cells receive their data from calculation results when clicking the TkFil button.

The issue I am facing is that the first four or eight cells of the row get their colors in a random order while all the rest behind are fine. I simply fail to understand and need some help: why is the colouring not working?

This is my full code:

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
1) The code has not been formatted properly - it is almost as if it has been copied from an email because there are spurious ">" characters in there. Please try again but this time copy the code from Visual Studio. 2) I suspect that it should not have i += 1 and j += 1 in the for n = ... loop.Andrew Morton
@Andrew Morton: (1) Sorry, I hope the format is now correct. (2) There are 3 different loops: For i, For j, For n. If i delete the first two ones, I just get the value of the last cell all over the whole row.Dom
You're using strings to compare what should be numbers. That can lead to unexpected results. If you use Dim a As Double = CDbl(Form36.dgv1.Rows(j).Cells(i).Value) and Dim b As Double = CDbl(Form36.dgv1.Rows(j - 1).Cells(i).Value) does it work?Andrew Morton
That was it, thank you so much. I'll take more care of Option Strict proposals next time. Where shall I click for your scoring system.Dom
I rewrote my comment as an answer for you - there should be something like a hollow green tick mark that you can click to make it an accepted answer, although you might have to wait a while before it can be ticked.Andrew Morton

1 Answers

0
votes

You're using strings to compare what should be numbers. That can lead to unexpected results, for example 8 < 10 but "8" > "10"

You just need to modify the code in PART(2) to

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)