Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB.NET
|
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 June 12th, 2003, 09:56 AM
Authorized User
 
Join Date: Jun 2003
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
 
Old June 19th, 2003, 03:53 PM
Registered User
 
Join Date: Jun 2003
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)
 
Old April 6th, 2004, 11:33 AM
Registered User
 
Join Date: Apr 2004
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)
 
Old April 6th, 2004, 11:43 AM
Registered User
 
Join Date: Jun 2003
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.