Inherited DataGridTextBoxColumn not highlighting different colored columns.

  • Thread starter Thread starter Sam
  • Start date Start date
S

Sam

I inherited the DataGridTextBoxColumn, so I could have some columns be
different colors. The Inherited class is this:

*********************************************************
Public Class DGTextBoxColumn
Inherits DataGridTextBoxColumn

Private m_objBackColor As Color
Private m_objForeColor As Color
'Private m_objRealValue As New Object
'Private m_strPounds As String

Public Sub New(ByVal BackColor As Color, ByVal ForeColor As Color)
MyBase.New()
m_objBackColor = BackColor
m_objForeColor = ForeColor
End Sub

Public Sub New()
MyBase.New()
End Sub

'-----------------------------------------------------------
'PURPOSE: Override the Paint method in order to use back and
' fore colors if set, by passing new brushes to the
' the mybase paint method.
'-----------------------------------------------------------
Protected Overloads Overrides Sub Paint( _
ByVal g As System.Drawing.Graphics, _
ByVal bounds As System.Drawing.Rectangle, _
ByVal source As System.Windows.Forms.CurrencyManager, _
ByVal rowNum As Integer, _
ByVal backBrush As System.Drawing.Brush, _
ByVal foreBrush As System.Drawing.Brush, _
ByVal alignToRight As Boolean)

'If the back/fore colors have been set, then use those
'to paint the textbox, otherwise, use the default colors.

If (m_objBackColor.ToArgb = 0) Then
MyBase.Paint(g, bounds, source, rowNum, backBrush, _
foreBrush, alignToRight)
Else
MyBase.Paint(g, bounds, source, rowNum, _
New SolidBrush(m_objBackColor), _
New SolidBrush(m_objForeColor), alignToRight)
End If
End Sub
End Class
*********************************************************

The problem is, when I select a row, some of the columns get
highlighted and some don't. The ones that get highlighted are the
columns that uses the New with no parameters (default colors and
default backBrush and foreBrush in MyBase.Paint method). The ones
that don't get highlighted are the ones that use the New where colors
are passed.

I've tried setting the TableStyle SelectionBackColor and
SelectionForeColor to something, and that color is then used, but the
columns with specific colors are still not highlighted when the row is
selected. Anyone have any ideas on how to get the columns with
specified back/fore colors to highlight too?

Thanks...
 
Hi Sam,

As far as I remember, the Paint method has several overloads, and different
overloads are called under different circumstances. I'd suggest checking
that you have overridden all the required overloads (MSDN seems to outline
when each of the overloads is used).
 
Thanks for the response(s). I found a way to do it without adding too
much code. What was happening was that I was overriding the colors in
the paint method even when the colors passed were the highlighting
colors.

************************************************************
Public Class DGTextBoxColumn
Inherits DataGridTextBoxColumn

Private m_objBackColor As Color
Private m_objForeColor As Color
Private m_objBaseBackColor As Color

Public Sub New(ByVal BackColor As Color, ByVal ForeColor As Color)
MyBase.New()
m_objBackColor = BackColor
m_objForeColor = ForeColor
m_objBaseBackColor = TextBox.BackColor
End Sub

Public Sub New()
MyBase.New()
m_objBaseBackColor = TextBox.BackColor
End Sub

'-----------------------------------------------------------
'PURPOSE: Override the Paint method in order to use back and
' fore colors if set, by passing new brushes to the
' the mybase paint method.
'-----------------------------------------------------------
Protected Overloads Overrides Sub Paint( _
ByVal g As System.Drawing.Graphics, _
ByVal bounds As System.Drawing.Rectangle, _
ByVal source As System.Windows.Forms.CurrencyManager, _
ByVal rowNum As Integer, _
ByVal backBrush As System.Drawing.Brush, _
ByVal foreBrush As System.Drawing.Brush, _
ByVal alignToRight As Boolean)

Dim BackColor As Color
BackColor = CType(backBrush, SolidBrush).Color

'If the back/fore colors have been set, then use those
'to paint the textbox, otherwise, use the default
'colors. If the Base Back color is not the same
'as the color of the backBrush, then draw normally.
'This is so the cell is drawn correctly when the row
'is selected.
If (m_objBackColor.ToArgb = 0 OrElse _
BackColor.ToArgb <> m_objBaseBackColor.ToArgb) Then
MyBase.Paint(g, bounds, source, rowNum, backBrush, _
foreBrush, alignToRight)
Else
MyBase.Paint(g, bounds, source, rowNum, _
New SolidBrush(m_objBackColor), _
New SolidBrush(m_objForeColor), alignToRight)
End If
End Sub
End Class
************************************************************

Thanks again...
 
Back
Top