Arg81
Well-known member
Below is some code I use to loop through rows in a datagridview and set the row colour subject to a "STATUS" field and also changing font colour and boldness dependant on whether a flag has been set to true.
Is this code optimal / can it be cleaned up somewhat?
Is this code optimal / can it be cleaned up somewhat?
VB.NET:
Public Sub ColourMyGrid()
Dim varStatus, varBagTemp As Integer
Dim myFont As New Font(grdResults.Font, FontStyle.Bold)
For Each row As DataGridViewRow In Me.grdResults.Rows
varStatus = row.Cells.Item("Status").Value
varBagTemp = row.Cells.Item("BagNo").Value
If varStatus = "1" Then
row.DefaultCellStyle.BackColor = Color.LightGreen
varGoodBagNo = varBagTemp
ElseIf varStatus = "2" Then
row.DefaultCellStyle.BackColor = Color.PaleGoldenrod
varGoodBagNo = varBagTemp
If Convert.ToBoolean(row.Cells.Item("OOS_mo").Value) = True Then
row.Cells.Item("RangeMoisture").Style.Font = myFont
row.Cells.Item("RangeMoisture").Style.ForeColor = Color.White
End If
If Convert.ToBoolean(row.Cells.Item("OOS_90").Value) = True Then
row.Cells.Item("Range90").Style.Font = myFont
row.Cells.Item("Range90").Style.ForeColor = Color.White
End If
If Convert.ToBoolean(row.Cells.Item("OOS_56").Value) = True Then
row.Cells.Item("Range56").Style.Font = myFont
row.Cells.Item("Range56").Style.ForeColor = Color.White
End If
If Convert.ToBoolean(row.Cells.Item("OOS_335").Value) = True Then
row.Cells.Item("Range335").Style.Font = myFont
row.Cells.Item("Range335").Style.ForeColor = Color.White
End If
If Convert.ToBoolean(row.Cells.Item("OOS_17").Value) = True Then
row.Cells.Item("Range17").Style.Font = myFont
row.Cells.Item("Range17").Style.ForeColor = Color.White
End If
If Convert.ToBoolean(row.Cells.Item("OOS_pan").Value) = True Then
row.Cells.Item("RangePan").Style.Font = myFont
row.Cells.Item("Rangepan").Style.ForeColor = Color.White
End If
ElseIf varStatus = "3" Then
row.DefaultCellStyle.BackColor = Color.IndianRed
If Convert.ToBoolean(row.Cells.Item("OOS_mo").Value) = True Then
row.Cells.Item("RangeMoisture").Style.Font = myFont
row.Cells.Item("RangeMoisture").Style.ForeColor = Color.White
End If
If Convert.ToBoolean(row.Cells.Item("OOS_90").Value) = True Then
row.Cells.Item("Range90").Style.Font = myFont
row.Cells.Item("Range90").Style.ForeColor = Color.White
End If
If Convert.ToBoolean(row.Cells.Item("OOS_56").Value) = True Then
row.Cells.Item("Range56").Style.Font = myFont
row.Cells.Item("Range56").Style.ForeColor = Color.White
End If
If Convert.ToBoolean(row.Cells.Item("OOS_335").Value) = True Then
row.Cells.Item("Range335").Style.Font = myFont
row.Cells.Item("Range335").Style.ForeColor = Color.White
End If
If Convert.ToBoolean(row.Cells.Item("OOS_17").Value) = True Then
row.Cells.Item("Range17").Style.Font = myFont
row.Cells.Item("Range17").Style.ForeColor = Color.White
End If
If Convert.ToBoolean(row.Cells.Item("OOS_pan").Value) = True Then
row.Cells.Item("RangePan").Style.Font = myFont
row.Cells.Item("Rangepan").Style.ForeColor = Color.White
End If
End If
Next
End Sub