Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB.NET
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 12th, 2003, 09:56 AM
Authorized User
 
Join Date: Jun 2003
Location: Richardson, Texas, USA.
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default Windows Forms datagrid column backcolor

Does anyone know how to change the backcolor of one column in a windows forms datagrid?

Thanks,

David
Reply With Quote
  #2 (permalink)  
Old June 19th, 2003, 03:53 PM
Registered User
 
Join Date: Jun 2003
Location: St. Petersburg, FL, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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)
Reply With Quote
  #3 (permalink)  
Old April 6th, 2004, 11:33 AM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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)
Reply With Quote
  #4 (permalink)  
Old April 6th, 2004, 11:43 AM
Registered User
 
Join Date: Jun 2003
Location: St. Petersburg, FL, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
windows forms lakshmiR .NET Framework 1.x 1 August 31st, 2007 08:32 PM
Datagrid: cell backcolor kesar ASP.NET 2.0 Professional 1 January 12th, 2007 01:54 PM
windows forms datagrid - refresh ab_dev General .NET 0 March 1st, 2006 06:37 PM
windows forms chandrasekhar ASP.NET 2.0 Professional 2 February 27th, 2006 10:41 PM
Windows Forms DataGrid .NET antalpak VS.NET 2002/2003 2 June 20th, 2003 08:09 AM



All times are GMT -4. The time now is 03:34 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.