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
i += 1
andj += 1
in thefor n = ...
loop. – Andrew MortonDim a As Double = CDbl(Form36.dgv1.Rows(j).Cells(i).Value)
andDim b As Double = CDbl(Form36.dgv1.Rows(j - 1).Cells(i).Value)
does it work? – Andrew Morton