This might not be everything you need, but it's got the guts of it. You
have to inhert from the columnstyle and overwrite the paint method involved.
There are 3 paint methods that can be called. Hope this helps. I think
there are other ways to do it, but this is the way I have always done it.
Public Class MyDataGridColumnStyle
Inherits DataGridColumnStyle
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _
ByVal Bounds As Rectangle, _
ByVal Source As CurrencyManager,
_
ByVal RowNum As Integer)
Paint(g, Bounds, Source, RowNum, False)
End Sub
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _
ByVal Bounds As Rectangle, _
ByVal Source As CurrencyManager,
_
ByVal RowNum As Integer, _
ByVal AlignToRight As Boolean)
Dim Text As String = GetText(GetColumnValueAtRow(Source, RowNum))
Dim DR As DataRow
DR = DirectCast(Source.Current,
DataRowView).DataView.Item(RowNum).Row
If SomeConditionIsMet Then
Dim BackBrush As Brush = New
SolidBrush(Me.DataGridTableStyle.BackColor)
Dim ForeBrush As Brush = New
SolidBrush(System.Drawing.Color.Salmon)
PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight)
ElseIf SomeOtherConiditionWasMet Then
Dim BackBrush As Brush = New
SolidBrush(Me.DataGridTableStyle.BackColor)
Dim ForeBrush As Brush = New
SolidBrush(System.Drawing.Color.RosyBrown)
PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight)
Else
PaintText(g, Bounds, Text, AlignToRight)
End If
End Sub
Protected Overloads Sub Paint(ByVal g As Graphics, _
ByVal Bounds As Rectangle, _
ByVal Source As CurrencyManager, _
ByVal RowNum As Integer, _
ByVal BackBrush As Brush, _
ByVal ForeBrush As Brush, _
ByVal AlignToRight As Boolean)
Dim Text As String = GetText(GetColumnValueAtRow(Source, RowNum))
PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight)
End Sub
Private Sub PaintText(ByVal g As Graphics, _
ByVal Bounds As Rectangle, _
ByVal Text As String, _
ByVal AlignToRight As Boolean)
Dim BackBrush As Brush = New
SolidBrush(Me.DataGridTableStyle.BackColor)
Dim ForeBrush As Brush = New
SolidBrush(Me.DataGridTableStyle.ForeColor)
PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight)
End Sub
Private Sub PaintText(ByVal g As Graphics, _
ByVal TextBounds As Rectangle, _
ByVal Text As String, _
ByVal BackBrush As Brush, _
ByVal ForeBrush As Brush, _
ByVal AlignToRight As Boolean)
Dim Rect As Rectangle = TextBounds
Dim RectF As RectangleF = RectangleF.op_Implicit(Rect) ' Convert to
RectangleF
Dim Format As StringFormat = New StringFormat
If AlignToRight Then
Format.FormatFlags = StringFormatFlags.DirectionRightToLeft
End If
Select Case Me.Alignment
Case Is = HorizontalAlignment.Left
Format.Alignment = StringAlignment.Near
Case Is = HorizontalAlignment.Right
Format.Alignment = StringAlignment.Far
Case Is = HorizontalAlignment.Center
Format.Alignment = StringAlignment.Center
End Select
Format.FormatFlags = Format.FormatFlags Or StringFormatFlags.NoWrap
g.FillRectangle(Brush:=BackBrush, Rect:=Rect)
Rect.Offset(0, yMargin)
Rect.Height -= yMargin
g.DrawString(Text, Me.DataGridTableStyle.DataGrid.Font, ForeBrush,
RectF, Format)
Format.Dispose()
End Sub
End Class
"Mad Scientist Jr" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Can someone post a clear example of how to change the background color
> of an individual datagrid cell for a Windows desktop app? (preferably
> in vb.net)
>
> I found some code on how to do this when the user clicks on the cell:
>
> Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object,
> ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
> DataGrid1.Controls(DataGrid1.CurrentCell.ColumnNumber +
> 2).BackColor = System.Drawing.Color.Blue
> DataGrid1.Controls(DataGrid1.CurrentCell.ColumnNumber +
> 2).ForeColor = System.Drawing.Color.White
> End Sub
>
> I tried setting this property outside the event, but the color isn't
> visible until the cell is clicked on:
>
> Dim DataGridCell2 As New DataGridCell
> DataGridCell2.ColumnNumber = 1
> DataGridCell2.RowNumber = 1
> DataGrid1.Controls(DataGridCell2.ColumnNumber + 2).BackColor =
> System.Drawing.Color.Blue
> DataGrid1.Controls(DataGridCell2.ColumnNumber + 2).ForeColor =
> System.Drawing.Color.White
>
> I found a post with code, saying you have to override the paint event.
> I tried pasting this in my project, but haven't gotten it to work (see
> below)
>
> Any help appreciated !
>
> Public Class DataGridColoredTextBoxColumn
> Inherits DataGridTextBoxColumn
>
> Private m_backColor As Color
> Private m_foreColor As Color
>
> Public Property BackColor() As Color
> Get
> If m_backColor.IsEmpty Then
> m_backColor = DataGridTableStyle.BackColor
> End If
> Return m_backColor
> End Get
> Set(ByVal Value As Color)
> m_backColor = Value
> End Set
> End Property
>
> Public Property ForeColor() As Color
> Get
> If m_foreColor.IsEmpty Then
> m_foreColor = DataGridTableStyle.ForeColor
> End If
> Return m_foreColor
> End Get
> Set(ByVal Value As Color)
> m_foreColor = Value
> End Set
> End Property
>
> Private Function GetText(ByVal Value As Object) As String
> If TypeOf (Value) Is System.DBNull Then
> Return NullText
> ElseIf Value Is Nothing Then
> Return ""
> Else
> Return Value.ToString
> End If
> End Function
>
> 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)
> MyClass.Paint(g, bounds, source, rowNum, False)
> End Sub
>
> 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 alignToRight As
> Boolean)
> Dim backBrush As Brush
> Dim foreBrush As Brush
> backBrush = New SolidBrush(BackColor)
> foreBrush = New SolidBrush(ForeColor)
> MyClass.Paint(g, bounds, source, rowNum, backBrush,
> foreBrush, alignToRight)
> End Sub
>
> 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 text As String
> text = GetText(GetColumnValueAtRow(source, rowNum))
> backBrush = New SolidBrush(BackColor)
> foreBrush = New SolidBrush(ForeColor)
> MyBase.PaintText(g, bounds, text, backBrush, foreBrush,
> alignToRight)
> End Sub
>
> Public Overrides Property [ReadOnly]() As Boolean
> Get
> Return True
> End Get
> Set(ByVal Value As Boolean)
> Throw New NotSupportedException("ReadOnly property is
> not changable!")
> End Set
> End Property
>
> End Class