asp.netvb.netrowdatabound

I want to make the same condition on all row data bound cell , How to do it?


I want to make all of the cell have their own color

eg,

  1. status = v ---> color Yellow
  2. status = P.H ---> Color Green
  3. status = H -----> Blue
  4. else status ----> Red

This is the code of that situation

Protected Sub gvMonthlyReport_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.Header Then
        e.Row.CssClass = "HeaderRow"
        e.Row.HorizontalAlign = HorizontalAlign.Center
        e.Row.VerticalAlign = VerticalAlign.Middle
    End If
    'If e.Row.RowType = DataControlRowType.DataRow Then
    '    e.Row.CssClass = "cellRow"
    'End If

    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim cell As TableCell = e.Row.Cells(17)
        Dim status As String = cell.Text

        If status = "v" Then
            cell.BackColor = Color.Yellow

        ElseIf status = "H" Then
            cell.BackColor = Color.Blue

        ElseIf status = "P.H" Then
            cell.BackColor = Color.Green

        Else
            cell.BackColor = Color.Red
        End If
    End If

End Sub

this code i have done give me the output color only one column like this image

output image

actually i want all column , can someone show me?


Solution

  • You have hardcoded cell number 17, but you need to use a loop in the RowDataBound event.

    If (e.Row.RowType = DataControlRowType.DataRow) Then
    
        Dim i As Integer = 0
    
        Do While (i < e.Row.Cells.Count)
            Dim cell As TableCell = CType(e.Row.Cells(i),TableCell)
            cell.BackColor = Color.Red
            i = (i + 1)
        Loop    
    
    End If