Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 我应该如何在DataGridview中的特定结果中添加颜色。如果工作状态为绿色,如果不工作,则为红色_Vb.net - Fatal编程技术网

Vb.net 我应该如何在DataGridview中的特定结果中添加颜色。如果工作状态为绿色,如果不工作,则为红色

Vb.net 我应该如何在DataGridview中的特定结果中添加颜色。如果工作状态为绿色,如果不工作,则为红色,vb.net,Vb.net,如果“状态”为“正在工作”,而“未工作”为红色,我应该如何在特定结果中添加颜色?我尝试了一些代码,但我仍然得到相同的颜色,或者它不工作 我的代码: LblMessage.Text += "Working" dt_row_result = dt_result.NewRow() dt_row_result("URL") = PROTECTED_URL dt_row_result("Status") = LblMessage.Text.

如果“状态”为“正在工作”,而“未工作”为红色,我应该如何在特定结果中添加颜色?我尝试了一些代码,但我仍然得到相同的颜色,或者它不工作

我的代码:

        LblMessage.Text += "Working"
        dt_row_result = dt_result.NewRow()

        dt_row_result("URL") = PROTECTED_URL
        dt_row_result("Status") = LblMessage.Text.ToString()
        dt_row_result("Checked Time") = DateTime.Now()
        dt_row_result("Comments") = "No Issue"
        dt_result.Rows.Add(dt_row_result.ItemArray)
        DataGrid.DataSource = dt_result
        For i As Integer = 0 To dt_result.Rows.Count() - 1
            DataGrid.Rows(i).DefaultCellStyle.BackColor = Color.Green

        Next
    End If

    Continue For
Catch ee As Exception
    If dt_row_result Is Nothing Then
        dt_row_result = dt_result.NewRow()

    End If
    dt_row_result = dt_result.NewRow()
    LblMessage.Text = "Not Working"
    dt_row_result("URL") = PROTECTED_URL
    dt_row_result("Status") = LblMessage.Text.ToString()
    dt_row_result("Checked Time") = DateTime.Now()
    dt_row_result("Comments") = "Issue"
    dt_result.Rows.Add(dt_row_result.ItemArray)
    DataGrid.DataSource = dt_result

    For i As Integer = 0 To dt_result.Rows.Count() - 1
        DataGrid.Rows(i).DefaultCellStyle.BackColor = Color.Red

    Next

您可以创建一个在gridview的行数据绑定上运行的方法。然后检查状态并将单元格颜色设置为所需颜色

很难判断代码在做什么,因为缺少块状态和停止。但是您不需要循环通过DGV行。使用RowPrePaint或CellFormatting事件instead@Plutonix很好的一天。我可以知道RowPrePaint做什么吗?系统正在检查url是否正常工作。我尝试了一些代码,但如果状态不起作用,则绿色显示在其上,一旦不起作用,DataGrid中的列中将显示为红色。请告知。你好,你好,谢谢回复。我检查一下,我这边做谢谢
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting

Dim drv As DataRowView
If e.RowIndex >= 0 Then
    If e.RowIndex <= ds.Tables("TableName").Rows.Count - 1 Then
        drv = ds.Tables("TableName").DefaultView.Item(e.RowIndex)
        Dim c As Color

        If drv.Item("FieldName").ToString = "N" Then
            c = Color.Green
        Else
            c = Color.Red
        End If
        Me.DataGridView1.Rows(e.RowIndex).Cells("FieldName").Style.BackColor = c
    End If
End If
End Sub
  Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGrid.CellFormatting
    For i As Integer = 0 To dt_result.Rows.Count() - 1

        If dt_result.Rows.Count() - 0 Then
            DataGrid.Rows(i).DefaultCellStyle.BackColor = Color.Green
        End If

    Next
    Dim drv As DataRowView
    If e.RowIndex >= 0 Then
        If e.RowIndex <= dt_result.Rows.Count - 1 Then
            drv = dt_result.DefaultView.Item(e.RowIndex)
            Dim c As Color
            If drv.Item("Status").ToString = "Working" Then
                c = Color.Green
            Else
                c = Color.Red
            End If
            e.CellStyle.BackColor = c
        End If
    End If
    DataGrid.DataSource = dt_result
    Dim drv As DataRowView
    Dim drvs = dt_result

End Sub