![](../images/header/spacer.gif) |
VB.NET General VB.NET discussions for issues that don't fall into other VB.NET forums. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
![Old](../images/statusicon/post_old.gif)
June 12th, 2003, 09:56 AM
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Windows Forms datagrid column backcolor
Does anyone know how to change the backcolor of one column in a windows forms datagrid?
Thanks,
David
|
![Old](../images/statusicon/post_old.gif)
June 19th, 2003, 03:53 PM
|
Registered User
|
|
Join Date: Jun 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
add this class to a module:
Public Class DataGridColoredTextBoxColumn
Inherits DataGridTextBoxColumn
Public Sub New()
End Sub
Protected Overloads Overrides 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)
' the idea is to conditionally set the foreBrush and/or backbrush
' depending upon some crireria on the cell value
' Here, we color anything that begins with a letter higher than 'F'
Try
Dim o As Object
o = Me.GetColumnValueAtRow(source, rowNum)
If (Not (o) Is Nothing) Then
Dim c As Char
c = CType(o, String).Substring(0, 1)
If (c > "F") Then
' could be as simple as
backBrush = New SolidBrush(Color.Pink)
' or something fancier...
'backBrush = New LinearGradientBrush(bounds, Color.FromArgb(255, 200, 200), Color.FromArgb(128, 20, 20), LinearGradientMode.BackwardDiagonal)
foreBrush = New SolidBrush(Color.White)
End If
End If
Catch ex As Exception
' empty catch
Finally
' make sure the base class gets called to do the drawing with
' the possibly changed brushes
MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
End Try
End Sub
End Class
then add it to your tablestyle for your grid:
Dim newcol As New colorcolumn(Color.LightYellow, Color.Black)
Me.gridmanagers.TableStyles(0).GridColumnStyles.Ad d(newcol)
|
![Old](../images/statusicon/post_old.gif)
April 6th, 2004, 11:33 AM
|
Registered User
|
|
Join Date: Apr 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Referencing your last line, where did you define 'colorcolumn', as in Dim newcol As New colorcolumn(Color.LightYellow, Color.Black)
?
Quote:
quote:Originally posted by whammoed
add this class to a module:
Public Class DataGridColoredTextBoxColumn
Inherits DataGridTextBoxColumn
Public Sub New()
End Sub
Protected Overloads Overrides 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)
' the idea is to conditionally set the foreBrush and/or backbrush
' depending upon some crireria on the cell value
' Here, we color anything that begins with a letter higher than 'F'
Try
Dim o As Object
o = Me.GetColumnValueAtRow(source, rowNum)
If (Not (o) Is Nothing) Then
Dim c As Char
c = CType(o, String).Substring(0, 1)
If (c > "F") Then
' could be as simple as
backBrush = New SolidBrush(Color.Pink)
' or something fancier...
'backBrush = New LinearGradientBrush(bounds, Color.FromArgb(255, 200, 200), Color.FromArgb(128, 20, 20), LinearGradientMode.BackwardDiagonal)
foreBrush = New SolidBrush(Color.White)
End If
End If
Catch ex As Exception
' empty catch
Finally
' make sure the base class gets called to do the drawing with
' the possibly changed brushes
MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
End Try
End Sub
End Class
then add it to your tablestyle for your grid:
Dim newcol As New colorcolumn(Color.LightYellow, Color.Black)
Me.gridmanagers.TableStyles(0).GridColumnStyles.Ad d(newcol)
|
|
![Old](../images/statusicon/post_old.gif)
April 6th, 2004, 11:43 AM
|
Registered User
|
|
Join Date: Jun 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
sorry, that was the original class I modified...here is the one I ended up using:
Public Class colorcolumn
Inherits DataGridTextBoxColumn
Private bcolor As Color
Private fcolor As Color
Public Sub New(ByVal enterbackcolor As Color, ByVal enterforecolor As Color)
Me.bcolor = enterbackcolor
Me.fcolor = enterforecolor
End Sub
Protected Overloads Overrides 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)
' the idea is to conditionally set the foreBrush and/or backbrush
' depending upon some crireria on the cell value
' Here, we color anything that begins with a letter higher than 'F'
Try
' could be as simple as
backBrush = New SolidBrush(bcolor)
' or something fancier...
'backBrush = New LinearGradientBrush(bounds, Color.FromArgb(255, 200, 200), Color.FromArgb(128, 20, 20), LinearGradientMode.BackwardDiagonal)
foreBrush = New SolidBrush(fcolor)
Catch ex As Exception
' empty catch
Finally
' make sure the base class gets called to do the drawing with
' the possibly changed brushes
MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
End Try
End Sub
End Class
|
|
![](../images/header/spacer.gif) |