Separate Font Colors per Row in DataGrid

Is there a way to specify the ForeColor (font color) of a given row in a
datagrid (MS datagrid)?
hquinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VBGuruCommented:
Are you talking of DBGrid?
0
VBGuruCommented:
Use the True DB grid's FetchCellStyle event to do that.
Only pre-requist is that the 1st column is a column with the serial number. This is needed for deciding the color of the row. As shown below.
Private Sub TDBGrid1_FetchCellStyle(ByVal Condition As Integer, ByVal Split As Integer, Bookmark As Variant, ByVal Col As Integer, ByVal CellStyle As TrueDBGrid50.StyleDisp)
   
    ' FetchCellStyle is the event we need to use
    ' to affect the colors of individual cells
    ' based on custom criteria.

    On Error GoTo done:
    Select Case Col
        Case 1
'Assume that the first column contain the serial numbers, Just make thecalucalation of
'Even and odd number and assign the color to the rows.
            Dim N As Long

            N = Val(TDBGrid1.Columns(Col).CellText(Bookmark))
            If (N / 2) = (N \ 2) Then        'If the row is a even or odd
                CellStyle.BackColor = vbGreen
                CellStyle.ForeColor = vbWhite
            Else
                CellStyle.BackColor = vbRed
                CellStyle.ForeColor = vbWhite
            End If
        Case 3
            ' For all customers who     End Select

Exit Sub

done:


End Sub





------------

Note:The FetchCellStyle event occurs when the grid is about to display cell data in a column whose FetchStyle property is set to True.  By setting one or more properties of the Style object passed in the CellStyle parameter, your application can change the appearance of individual cells.


---------------
Syntax      TDBGrid_FetchCellStyle (ByVal Condition As Integer, ByVal Split As Integer, Bookmark As Variant, ByVal Col As Integer, ByVal CellStyle As TrueDBGrid50.StyleDisp)
      TDBDropDown_FetchCellStyle (ByVal Condition As Integer, Bookmark As Variant, ByVal Col As Integer, ByVal CellStyle As TrueDBGrid50.StyleDisp)

Event applies to TDBGrid and TDBDropDown controls.

Arguments      Condition is the sum of one or more CellStyleConstants describing the disposition of the cell being displayed.  

Split is an integer that identifies the split containing the cell being displayed.  This argument is omitted for TDBDropDown controls.

Bookmark is a variant that identifies the row containing the cell being displayed.

Col is an integer that identifies the column containing the cell being displayed.

CellStyle is a Style object used to override the font and color characteristics of the cell being displayed.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.