我有一个像这样的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)
我希望我的DataGridView在Value为1时有红色行。我该怎么做?我在VB.NET工作。
我在其中一个答案中尝试了代码,它看起来像这样:
答案 0 :(得分: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
如果这不起作用,请告诉我。
答案 1 :(得分: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
答案 2 :(得分:1)
谢谢Senthilkumar 如果Text等于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
答案 3 :(得分:1)
'使用此代码更改'datagridview
的cellformatting事件中特定行的颜色 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
答案 4 :(得分: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
答案 5 :(得分:0)
这是我的代码:
For Each row As GridViewRow In GridView1.Rows
row.BackColor = Drawing.Color.White
Next row