DataGridView.RowPrePaint - how to selectively paint the background

G

Guest

Hi,

I am trying to change the background colour of the entire row based on the
contents of a value in a cell.

Using the examples in the MSDN documentation I have got thus far, however,
the foreground text is garbled:

Private Sub DataGridView1_RowPrePaint(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles
DataGridView1.RowPrePaint
Dim backBrush As New
System.Drawing.SolidBrush(Me.GetRowColour(CType(sender, DataGridView),
e.RowIndex))
Dim rowBounds As Rectangle = GetRowBounds(CType(sender,
DataGridView), e.RowBounds)
Try
e.Graphics.FillRectangle(backBrush, rowBounds)
e.PaintParts = DataGridViewPaintParts.All And Not
DataGridViewPaintParts.ContentBackground
Finally
backBrush.Dispose()
End Try
End Sub

Private Function GetRowBounds(ByVal Grid As DataGridView, ByVal
RowBounds As Rectangle) As Rectangle
Return New Rectangle(Grid.RowHeadersWidth, RowBounds.Top,
Grid.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) -
Grid.HorizontalScrollingOffset + 1, RowBounds.Height)
End Function

Private Function GetRowColour(ByVal Grid As DataGridView, ByVal rowIndex
As Integer) As Drawing.Color
Dim IsRoasted As Object = Grid.Rows(rowIndex).Cells(5).Value
Dim IsOrganic As Object = Grid.Rows(rowIndex).Cells(4).Value
Dim IsProduction As Object = Grid.Rows(rowIndex).Cells(3).Value
If CBool(IsRoasted) Then
Return Color.Red
ElseIf CBool(IsOrganic) Then
Return Color.Green
ElseIf CBool(IsProduction) Then
Return Color.Beige
End If
End Function

Could someone suggest what I missed here?

Many thanks
Jeremy Holt
 
B

Bart Mermuys

Hi,

JeremyHolt said:
Hi,

I am trying to change the background colour of the entire row based on the
contents of a value in a cell.

Using the examples in the MSDN documentation I have got thus far, however,
the foreground text is garbled:

It looks ok to me, i would only change a couple of things, instead of
excluding DGVPaintPart.ContentBackground i would exclude
DGVPaintPart.Background, because eg. DGVButtonCell paints its button for
DGVPaintPart.ContentBackground.

Second, more important i would add a Catch to see if any exceptions are
thrown, they can mess up the rest of the painting.

Private Sub DataGridView1_RowPrePaint(ByVal sender As System.Object, ByVal
e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles
DataGridView1.RowPrePaint
Try
If ( (e.State And DataGridViewElementStates.Selected) =
DataGridViewElementStates.None) Then
Dim backBrush As New
System.Drawing.SolidBrush(Me.GetRowColour(CType(sender, DataGridView),
e.RowIndex))
Dim rowBounds As Rectangle = _
GetRowBounds(CType(sender, DataGridView), e.RowBounds)

e.Graphics.FillRectangle(backBrush, rowBounds)
backBrush.Dispose()

e.PaintHeader( False ) ' because background is excluded
e.PaintParts = DataGridViewPaintParts.All And Not
DataGridViewPaintParts.Background
End If
Catch er As Exception
Console.WriteLine( er.Message ) ' watch if no exception occured
End Try
End Sub

HTH,
Greetings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top