473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

changing bg color of individual datagrid cell

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
3 15378
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dav | last post by:
I have a Winform with a derived datagrid and a button that adds a row to the datagrid in its Click event. The button has a shortcut text "&New Row". When I enter a value in a datagrid cell and...
2
by: Stephen | last post by:
I'm trying to work with a datagrid column in order to display a tooltip in a datagrid cell. The reason I am doing this is because I have some long strings being returned and I don't want the rows...
2
by: Nanda | last post by:
Hi all, while editing a datagrid cell, if i press Enter key focus goes to the next row, instead that i would like to have focus on next cell, how can i obtain this. Thanks & Regards, Nanda
1
by: VM | last post by:
Is it possible for the bound data in a web datagrid to be displayed in links? The grid will show the client's first name and last name and, when the user clicks on the first or last name, I want...
0
by: Michael Schindler | last post by:
How i can eliminate a cursor from a datagrid cell? If I click into a datagrid cell then I open a other dialog with parameter key from this row. If I close this other dialogue a can not reopen...
2
by: Tolga | last post by:
How do insert a user control i have created into a datagrid cell. Basically i am trying to create a column of my user controls. This is what i have tried so far. int i = 0; while (i <...
4
by: SStory | last post by:
I want to use F6,F7,etc for an entire form. Have key preview to true and it works everywhere but on the grid. On the grid sometimes it works and sometimes not. Appears that if I am in a cell...
9
by: robert_rowe | last post by:
Does anyone know how to detect keypresses in a Datagrid cell? The Keypress event ofthe datagrid doesn't fire if you are in a cell. Neither does KeyUp & KeyDown. I've tried hooking in to the...
3
by: Jeff | last post by:
Help.. I have been using: string sId = ((System.Web.UI.WebControls.TextBox)e.Item.Cells.Controls).Text; To get the value of a cell in my datagrid. When I set the cell to read only, I get...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.