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
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