Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing DataGridView cell border color based on cell value

Status
Not open for further replies.

Larry Seymour

Programmer
Oct 5, 2022
2
CA
Hello,
I have a screen with a DataGridView in it and I am populating each cell from a subpub object so as each subscription comes in I set the value of the appropriate cell. What I also want to do is change the border color of the cell if the value is FAIL. so if I do a
grid.Rows(0).Cells(3).Value = "FAIL"
set the border color to red.

How can I achieve this. Any help will be apricated. BTW, I can not switch from a pubsub subscription to another way of populaTING THE GRID.

lARRY
 
Here is one way to do it.

Myself, back in VB6, I just changed the text color of the cell.
Just a suggestions... :)

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Unfortunately that is not an option in this case as it goes against project standards.

---Larry
 
Larry Seymour said:
that is not an option in this case as it goes against project standards

Changing the text color of the cell or
drawing a red rectangle [as a] red border on the selected cells (from the link provided)?

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
You have to override the default cell painting activity for the datagrid. Roughly (very roughly) something like:

Code:
[COLOR=blue]Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
        Dim BrushWidth As Integer = 1
        Dim redPen As New Pen(Color.Red, BrushWidth)
        If e.Value = "Fail" Then
            Dim rect As New Rectangle(e.CellBounds.X + BrushWidth, e.CellBounds.Y + BrushWidth, e.CellBounds.Width - BrushWidth * 2, e.CellBounds.Height - BrushWidth * 2)
            e.Graphics.FillRectangle(New SolidBrush(e.CellStyle.BackColor), e.CellBounds)
            e.Graphics.DrawRectangle(redPen, rect)
            If e.Value <> "" Then e.Graphics.DrawString(e.Value, e.CellStyle.Font, Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y + 2, StringFormat.GenericDefault)
            e.Handled = True
        End If
    End Sub[/color]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top