3

我有一个像这样的 DataGridView: 在此处输入图像描述

我像这样绑定DataGridView:

Dim cmd As New SqlCommand("DashBordFetch", con.connect)  
cmd.CommandType = CommandType.StoredProcedure   
cmd.Parameters.Add("@locid", SqlDbType.Int).Value = locid
da.SelectCommand = cmd
da.Fill(ds)
DGVDashBoard.DataSource = ds.Tables(0)

当值为 1 时,我希望我的 DataGridView 具有红色行颜色。我该怎么做?我在 VB.NET 中工作。

我已经在提供的答案之一中尝试了代码,它看起来像这样:

4

6 回答 6

4

尝试这个

 Private Sub DGVDashBoard_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DGVDashBoard.CellFormatting
            Dim drv As DataRowView
            If e.RowIndex >= 0 Then
                ' Add Your condition here ignore following
                If e.RowIndex <= ds.Tables(0).Rows.Count - 1 Then
                    drv = ds.Tables(0).DefaultView.Item(e.RowIndex)
                    Dim c As Color
                    If drv.Item("Value").ToString = "1" Then
                        c = Color.Red                        
                    End If
                    e.CellStyle.BackColor = c
                End If
            End If
        End Sub

如果这不起作用,请告诉我。

于 2013-07-25T13:05:45.170 回答
2
Sub CustomersGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then

      If(your condition)
         e.Row.BackColor = Drawing.Color.Red 
      End If

    End If

  End Sub
于 2013-07-26T14:20:00.390 回答
1

'使用此代码更改'datagridview'的单元格格式化事件中特定行的颜色

        For Each drview As DataGridViewRow In dgrawPRoducts.Rows
            If drview.Cells.Count >= 14 Then
                If Not IsNothing(drview.Cells(14).Value) Then
                    If drview.Cells(14).Value.ToString.Trim = "N" Then
                        For i As Integer = 0 To drview.Cells.Count - 1
                            drview.Cells(i).Style.ForeColor = Color.Red
                           drview.Cells(i).Style.BackColor= Color.Gray
                        Next

                    End If
                End If
            End If

        Next
于 2015-03-18T05:52:50.887 回答
1

感谢 Senthilkumar,如果文本等于 Closed,我正在寻找可以让我为行背景着色的东西。这是我的最终解决方案,我只需要在值之后添加 ToString。DataGridviewTextBoxColumn16 名称来自 datagridview 属性。单击DataGridView 右上角的箭头,编辑列并找到名称值。希望对某人有所帮助。

For Each rw As DataGridViewRow In DataGridView1.Rows
        If rw.Cells("DataGridViewTextBoxColumn16").Value.ToString = "Closed" Then
            rw.DefaultCellStyle.BackColor = Color.Red
        Else
            rw.DefaultCellStyle.BackColor = Color.Green
        End If
    Next
于 2015-02-18T17:51:23.193 回答
0

这是我的代码:

For Each row As GridViewRow In GridView1.Rows
    row.BackColor = Drawing.Color.White
Next row
于 2015-04-28T08:22:56.347 回答
0
    For Each rw As DataGridViewRow In DataGridView1.Rows 
    If rw.Cells("slno").Value =1 Then 
    rw.DefaultCellStyle.BackColor = Color.Red
    Else 
    rw.DefaultCellStyle.BackColor = Color.Green
    End If 
    Next
于 2013-07-26T14:15:00.980 回答