Public Class FormProduct
Private Sub FormProduct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BindDataGrid()
For i As Integer = 0 To dgName.Rows.Count - 1
If dgName.Rows(i).Cells(2).Value < 1 Then
dgName.Rows(i).Cells(0).Style.BackColor = Color.White
dgName.Rows(i).Cells(1).Style.BackColor = Color.White
dgName.Rows(i).Cells(2).Style.BackColor = Color.White
dgName.Rows(i).Cells(2).Style.ForeColor = Color.Red
End If
If dgName.Rows(i).Cells(2).Value < 5 Then
dgName.Rows(i).Cells(0).Style.BackColor = Color.White
dgName.Rows(i).Cells(1).Style.BackColor = Color.White
dgName.Rows(i).Cells(2).Style.BackColor = Color.White
dgName.Rows(i).Cells(2).Style.ForeColor = Color.Yellow
End If
If dgName.Rows(i).Cells(2).Value > 5 Then
dgName.Rows(i).Cells(0).Style.BackColor = Color.White
dgName.Rows(i).Cells(1).Style.BackColor = Color.White
dgName.Rows(i).Cells(2).Style.BackColor = Color.White
dgName.Rows(i).Cells(2).Style.ForeColor = Color.Green
End If
Next i
End SubCode (VB.NET)
Public Class FormProduct
Private Sub FormProduct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BindDataGrid()
For i As Integer = 0 To dgName.Rows.Count - 1
If dgName.Rows(i).Cells(2).Value < 1 Then
dgName.Rows(i).Cells(0).Style.BackColor = Color.White
dgName.Rows(i).Cells(1).Style.BackColor = Color.White
dgName.Rows(i).Cells(2).Style.BackColor = Color.White
dgName.Rows(i).Cells(2).Style.ForeColor = Color.Red
ElseIf dgName.Rows(i).Cells(2).Value < 5 Then
dgName.Rows(i).Cells(0).Style.BackColor = Color.White
dgName.Rows(i).Cells(1).Style.BackColor = Color.White
dgName.Rows(i).Cells(2).Style.BackColor = Color.White
dgName.Rows(i).Cells(2).Style.ForeColor = Color.Yellow
ElseIf dgName.Rows(i).Cells(2).Value > 5 Then
dgName.Rows(i).Cells(0).Style.BackColor = Color.White
dgName.Rows(i).Cells(1).Style.BackColor = Color.White
dgName.Rows(i).Cells(2).Style.BackColor = Color.White
dgName.Rows(i).Cells(2).Style.ForeColor = Color.Green
End If
Next i
End Sub<strong>Code (VB.NET)</strong>
ถ้าให้ผมเดา อาจจะเป็นเพราะคุณใช้ if แล้ว endif ทันที ทำให้จะเข้าเช็คทุกเงื่อนไข ลองเปลี่ยนเป็น if elseif ดูนะครับ