By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,675 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,675 IT Pros & Developers. It's quick & easy.

changing bg color of individual datagrid cell

P: n/a
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.ColumnNum ber +
2).BackColor = System.Drawing.Color.Blue
DataGrid1.Controls(DataGrid1.CurrentCell.ColumnNum ber +
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <us*************@yahoo.com> wrote in message
news:7a**************************@posting.google.c om...
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.ColumnNum ber +
2).BackColor = System.Drawing.Color.Blue
DataGrid1.Controls(DataGrid1.CurrentCell.ColumnNum ber +
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

Nov 21 '05 #2

P: n/a
Okay, I have the class code, where do I put it? in the form's
codebehind? inside or outside the form's Class definition? In a separate
file? Also, how do i use it? i tried instantiating it but don't see any
methods / properties - and don't know how to attach it to the object I
am trying to extend (the datagrid). Any help or an example appreciated.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #3

P: n/a
I just realized that you are using ASP.NET aren't you (code-behind tiped me
off)? Sorry if I missed that earlier. That changes the ball game a lot.

This code here will alternate the back color of a row based in a datagrid
called CallDGrid. The key is the ItemDataBound, this gets called for each
row in the DataGrid. Now if you want to talk about one cell, do this code:
e.item.cells(#).BackColor. you can also use that e.item.cells to check the
value and then do whatever you want based on that.

Let me know if that helps
Chris
Dim X As Boolean

Private Sub CallDGrid_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
CallDGrid.ItemDataBound

X = Not X

If X Then

e.Item.BackColor = Color.Blue

Else

e.Item.BackColor = Color.Gold

End If

End Sub

"Emma Gumbdough" <us*************@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Okay, I have the class code, where do I put it? in the form's
codebehind? inside or outside the form's Class definition? In a separate
file? Also, how do i use it? i tried instantiating it but don't see any
methods / properties - and don't know how to attach it to the object I
am trying to extend (the datagrid). Any help or an example appreciated.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.