I have a DataGridView like this:

I am binding the DataGridView like this:
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)
I want to my DataGridView to have a red row color when the Value is 1. How can I do that? I am working in VB.NET.
I have tried the code in one of the answers provided, it looks like this:

Try this
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
Let me know if this doesn't work.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With