DatagridVIEW - set background for individual rows

OMRebel

Active member
Joined
Sep 27, 2006
Messages
44
Programming Experience
Beginner
I have a DataGridView on one of my forms. I populate the contents of that DGV with the following code:

VB.NET:
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = maintenance.mdb"
        con.Open()
        sql = "Select ID AS TicketNumber, shortDesc AS TicketDescription, techName AS AssignedTechnician, depName AS Department, Created AS DateOpened, lastUpdate AS LastUpdated, dueDate AS DateDue, prioName AS TicketPriority From Problem WHERE StatName <> 'Closed'"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Problem")
        con.Close()

        If ds.Tables("Problem").Rows.Count > 0 Then
            With dgvOpen
                .DataSource = ds.Tables("Problem")
            End With
        End If

What I'm wanting to do is to set the background color to Yellow if the "TicketPriority" is equal to a certain string (like "Critical"), but I'm not sure how to do that.

Any ideas?
 
Okay, I got it figured out - use the CellFormatting event:

VB.NET:
If Me.dgvOpen.Columns(e.ColumnIndex).Name _
        = "TicketPriority" Then
            If e.Value IsNot Nothing Then

                ' Check for the string "Critical" in the cell.
                Dim stringValue As String = _
                CType(e.Value, String)

                stringValue = stringValue.ToLower()

                If ((stringValue.IndexOf("critical") > -1)) Then
                    e.CellStyle.BackColor = Color.Red

                End If
                stringValue = String.Empty
            End If
        End If
 
Back
Top