我想在所有行数据绑定单元格上设置相同的条件,怎么办?

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

我想让所有的单元格都有自己的颜色

例如,

  1. status = v ---> color Yellow
  2. status = P.H ---> 颜色绿色
  3. status = H -----> 蓝色
  4. 其他状态---->红色

这是那个情况的代码

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

我完成的这段代码只给我输出颜色这样的一列 图片

output image

实际上我想要所有栏目,有人可以告诉我吗?

您已硬编码单元格编号 17,但您需要在 RowDataBound 事件中使用循环。

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