Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I change grids' row/column colors/fonts, ...

Grids

How do I change grids' row/column colors/fonts, ...

by  DSummZZZ  Posted    (Edited  )
1- Rows alternating color.
2- Different fonts.
3- Now how about different colors depending on value?
4- What? You want more color? Okay. Here it is.
5- Complete form code
Separate scenario, separate code
6- Hilight cells of grid for searched text
7- Complete form code

A few hints for highlighting grid rows or columns depending on the value.
Also, the font may be changed depending on the value. These are simple
examples and can be modified to fit your needs.

I have included the complete code at the end of the faq.


This FAQ uses a grid which has a control source of testtest.dbf, which is
based on the FOXUSER.DBF.
To create the testtest table, perform this query:
Code:
   SELECT * from SYS(2005) INTO TABLE testtest
Leave the table 'testtest' open and do these replaces, just to liven things
up a little:
Code:
REPLACE ALL type WITH 'INFO' FOR RECNO() % 3 = 0
REPLACE ALL type WITH 'JOE'  FOR RECNO() % 4 = 0
REPLACE ALL type WITH 'FRED' FOR RECNO() % 5 = 0

Create a form and drop a grid control on it, then set the RecordSource
property to testtest.

1- Rows alternating color.
Add this code to the grid's Init event:
Code:
This.column5.dynamicbackcolor = ;
   "IIF(MOD(recno(), 2) = 0, RGB(125,125,125), RGB(255,255,255))"
Run the form and you will see alternating rows in a different color.
As you can see, it's based on the RECNO() of the controlling table in
the grid, but can be based on a lot of other things like for instance,
a udf. Which is what we'll do next.

2- Different fonts.
Now to change the font depending on different values, you need to add
a small method to the form.

From the main menu, go to Form->New method.
In the name box, type: TypeFontBold

The bold font is based on what is in the "type" field, so that's the
naming convention for now. The people at Fox named it type, so don't
slam me about using a reserved word, okay? :)

Now edit that method by selecting it from the pull-down, or double
clicking on the name of the method in the 'Properties' dialog.
Add this code:
Code:
*...  Form method TypeFontBold  ...*
PARAMETERS search_string

IF VARTYPE(search_string) # 'C'
   RETURN .F.
ENDIF

DO CASE 
   CASE 'PRE' $ (search_string)
      RETURN .T.
      
   CASE 'INFO' $ (search_string)
      RETURN .T.
      
   OTHERWISE
      RETURN .F.

ENDCASE 
*... end TypeFontBold
Now add to the grid's init event:
Code:
This.column1.dynamicfontbold = ;
   "ThisForm.TypeFontBold(testtest.type)"
You want italic fonts too? Just add the same code as in the TypeFontBold
method to a new method named TypeFontItalic. Then to the grid's Init event,
add this:
Code:
This.column1.dynamicfontItalic = ;
   "ThisForm.TypeFontItalic(testtest.type)"

How about showing if a memo field has anything in it or not?
Add this to the grid Init event also:
Code:
This.column3.dynamicfontbold = ;
         "!EMPTY(testtest.name)"

3- Now how about different colors depending on value?
Add similar methods named say, TypeForeColor and TypeBackColor.
Code:
*... Form method TypeForeColor ...*
PARAMETERS search_string

DO CASE
   CASE search_string = 'PREFW'
      RETURN RGB(0,0,0) &&...black

   CASE search_string = 'INFO'
      RETURN RGB(0,0,0) &&...black

   CASE search_string = 'JOE'
      RETURN RGB(0,0,0) &&...black

   CASE search_string = 'FRED'
      RETURN RGB(0,0,0) &&...black

   OTHERWISE 
      RETURN RGB(255,255,255) &&...white

ENDCASE
*... end TypeForeColor ...*


*... Form method TypeBackColor ...*
PARAMETERS search_string

DO CASE
   CASE search_string = 'PREFW'
      RETURN RGB(255,0,0) &&... lite red

   CASE search_string = 'INFO'
      RETURN RGB(0,255,255) &&... lite blue

   CASE search_string = 'JOE'
      RETURN RGB(255,0,255) &&... fucia

   CASE search_string = 'FRED'
      RETURN RGB(0,255,0) &&... lite green

ENDCASE
*... end TypeBackColor ...*
Then add this code to the grid's Init event also:
Code:
This.column1.dynamicbackcolor = ;
   "ThisForm.TypeBackColor(testtest.type)"

This.column1.dynamicforecolor = ;
   "ThisForm.TypeForeColor(testtest.type)"
Experiment with changing the values. It's cool!


4- What? You want more color? Okay. Here it is.
Again, add form methods named say, IDForeColor and IDBackColor
and edit them the same as before, adding this code:
Code:
*... Form method IDBackColor...*
PARAMETERS search_string

DO CASE
   CASE search_string = 'PREFW'
      IF 'WIN' $ ID
         RETURN RGB(0,255,255) &&... lite blue
      ELSE
         RETURN RGB(0,0,128) &&... dark blue
      ENDIF

   CASE search_string = 'INFO'
      IF 'WIN' $ ID
         RETURN RGB(255,0,255) &&... fucia
      ELSE
         RETURN RGB(255,255,0) &&... yellow
      ENDIF

   CASE search_string = 'JOE'
      IF 'WIN' $ ID
         RETURN RGB(0,255,0) &&... lite green
      ELSE
         RETURN RGB(0,128,0) &&... dark green
      ENDIF

   CASE search_string = 'FRED'
      IF 'WIN' $ ID
         RETURN RGB(255,0,0) &&... lite red
      ELSE
         RETURN RGB(128,0,0) &&... dark red
      ENDIF

ENDCASE
*... end IDBackColor...*

*... Form method IDForeColor ...*
PARAMETERS search_string

DO CASE
   CASE search_string = 'PREFW'
      IF 'WIN' $ ID
         RETURN RGB(0,0,0) &&...black
      ELSE
         RETURN RGB(255,255,255) &&...white
      ENDIF

   CASE search_string = 'INFO'
      RETURN RGB(0,0,0) &&...black

   CASE search_string = 'JOE'
      IF 'WIN' $ ID
         RETURN RGB(0,0,0) &&...black
      ELSE
         RETURN RGB(255,255,255) &&... white
      ENDIF

   CASE search_string = 'FRED'
      IF 'WIN' $ ID
         RETURN RGB(0,0,0) &&...black
      ELSE
         RETURN RGB(255,255,255) &&... white
      ENDIF

ENDCASE
*... end IDForeColor...*
Then again, add this code to the grid's init event:
Code:
This.column2.dynamicbackcolor = ;
   "ThisForm.IDBackColor(testtest.type)"

This.column2.dynamicforecolor = ;
   "ThisForm.IDForeColor(testtest.type)"

Try changing values in column 1 and/or column 2. You should see
both columns react.

Of course, the literal string values can all be changed to different
values, or even udfs. This is just a brief(?) example as to how different
dynamic SetAll methods can work.

5- Complete form code
Here is the complete code you can cut and paste, to run the example:

Code:
***************************************************************
PUBLIC MultiColorGrid

SELECT * FROM SYS(2005) ;
   INTO TABLE TestTest
   
REPLACE ALL type WITH 'INFO' FOR RECNO() % 3 = 0
REPLACE ALL type WITH 'JOE'  FOR RECNO() % 4 = 0
REPLACE ALL type WITH 'FRED' FOR RECNO() % 5 = 0
GO TOP

MultiColorGrid = CREATEOBJECT("MultiColorGrid")
MultiColorGrid.Show
RETURN

DEFINE CLASS MultiColorGrid AS form


   Top = 0
   Left = 1
   Height = 274
   Width = 500
   DoCreate = .T.
   Caption = "Form1"
   Name = "Form1"


   ADD OBJECT grid1 AS grid WITH ;
      Height = 228, ;
      Left = 12, ;
      RecordSource = "testtest", ;
      Top = 24, ;
      Width = 490, ;
      Name = "Grid1"


   PROCEDURE typefontbold
      PARAMETERS search_string

      IF VARTYPE(search_string) # 'C'
         RETURN .F.
      ENDIF

      DO CASE 
         CASE 'PRE' $ (search_string)
            RETURN .T.
            
         CASE 'INFO' $ (search_string)
            RETURN .T.
            
         OTHERWISE
            RETURN .F.

      ENDCASE 
   ENDPROC


   PROCEDURE typefontitalic
      PARAMETERS search_string

      IF VARTYPE(search_string) # 'C'
         RETURN .F.
      ENDIF

      DO CASE 
         CASE 'PRE' $ (search_string)
            RETURN .T.
            
         CASE 'INFO' $ (search_string)
            RETURN .T.
            
         OTHERWISE
            RETURN .F.

      ENDCASE 
   ENDPROC


   PROCEDURE typebackcolor
      *FUNCTION BackTypeColor
      PARAMETERS search_string

      DO CASE
         CASE search_string = 'PREFW'
            RETURN RGB(255,0,0) &&... lite red

         CASE search_string = 'INFO'
            RETURN RGB(0,255,255) &&... lite blue

         CASE search_string = 'JOE'
            RETURN RGB(255,0,255) &&... fucia

         CASE search_string = 'FRED'
            RETURN RGB(0,255,0) &&... lite green

      ENDCASE
   ENDPROC


   PROCEDURE typeforecolor
      *FUNCTION ForeTypeColor
      PARAMETERS search_string

      DO CASE
         CASE search_string = 'PREFW'
            RETURN RGB(0,0,0) &&...black

         CASE search_string = 'INFO'
            RETURN RGB(0,0,0) &&...black

         CASE search_string = 'JOE'
            RETURN RGB(0,0,0) &&...black

         CASE search_string = 'FRED'
            RETURN RGB(0,0,0) &&...black

      ENDCASE
   ENDPROC


   PROCEDURE idforecolor

      *... Form method IDForeColor ...*
      PARAMETERS search_string

      DO CASE
         CASE search_string = 'PREFW'
            IF 'WIN' $ ID
               RETURN RGB(0,0,0) &&...black
            ELSE
               RETURN RGB(255,255,255) &&...white
            ENDIF

         CASE search_string = 'INFO'
            RETURN RGB(0,0,0) &&...black

         CASE search_string = 'JOE'
            IF 'WIN' $ ID
               RETURN RGB(0,0,0) &&...black
            ELSE
               RETURN RGB(255,255,255) &&... white
            ENDIF

         CASE search_string = 'FRED'
            IF 'WIN' $ ID
               RETURN RGB(0,0,0) &&...black
            ELSE
               RETURN RGB(255,255,255) &&... white
            ENDIF

      ENDCASE
      *... end IDForeColor...*
   ENDPROC


   PROCEDURE idbackcolor
      *... Form method IDBackColor...*
      PARAMETERS search_string

      DO CASE
         CASE search_string = 'PREFW'
            IF 'WIN' $ ID
               RETURN RGB(0,255,255) &&... lite blue
            ELSE
               RETURN RGB(0,0,128) &&... dark blue
            ENDIF

         CASE search_string = 'INFO'
            IF 'WIN' $ ID
               RETURN RGB(255,0,255) &&... fucia
            ELSE
               RETURN RGB(255,255,0) &&... yellow
            ENDIF

         CASE search_string = 'JOE'
            IF 'WIN' $ ID
               RETURN RGB(0,255,0) &&... lite green
            ELSE
               RETURN RGB(0,128,0) &&... dark green
            ENDIF

         CASE search_string = 'FRED'
            IF 'WIN' $ ID
               RETURN RGB(255,0,0) &&... lite red
            ELSE
               RETURN RGB(128,0,0) &&... dark red
            ENDIF

      ENDCASE
      *... end IDBackColor...*
   ENDPROC


   PROCEDURE grid1.Init

      This.column5.dynamicbackcolor = ;
         "IIF(MOD(recno(), 2) = 0, RGB(125,125,125), RGB(255,255,255))"

      This.column1.dynamicfontbold = ;
         "ThisForm.TypeFontBold(testtest.type)"

      This.column3.dynamicfontbold = ;
         "!EMPTY(testtest.name)"

      This.column1.dynamicfontItalic = ;
         "ThisForm.TypeFontItalic(testtest.type)"

      This.column1.dynamicbackcolor = ;
         "ThisForm.TypeBackColor(testtest.type)"

      This.column1.dynamicforecolor = ;
         "ThisForm.TypeForeColor(testtest.type)"
         
      This.column2.dynamicbackcolor = ;
         "ThisForm.IDBackColor(testtest.type)"

      This.column2.dynamicforecolor = ;
         "ThisForm.IDForeColor(testtest.type)"
   ENDPROC


ENDDEFINE
*
*-- EndDefine: testtest
**************************************************

6- Hilight cells of grid for searched text
Now let's say you want to hilight certain cells of a grid corresponding
to some search text. You can drop a grid control and a textbox control
on a form, then using the following procedure, cells of the grid which
contain the text within the textbox control will be hilighted. This
stuff is so cool. And fun.

Anyway, like I said. Drop a textbox control on the form and just leave
it the default size. Then add a grid control, and size it to about 500
wide and 200 high. Now in the control's Init event, put this code:
Code:
FOR jj = 1 TO This.ColumnCount
   STORE 'Column' + ALLTRIM(STR(jj)) TO MyColumn
   This.&MyColumn..dynamicbackcolor = ;
      "ThisForm.hilite(ALLTRIM(ThisForm.Text1.Value), " + ;
      "This.&MyColumn..controlsource)"
NEXT
Now, create a form method named 'HiLite', and add this code to it:

Code:
*... Form method hilite ...
PARAMETERS search_string, cfield

DO CASE
   CASE TYPE(cfield) = "C" OR TYPE(cfield) = "M" 
      IF search_string $ EVAL(cfield)
         RETURN RGB(0,255,255) &&... lite blue
      ENDIF

   CASE TYPE(cfield) = "N"
      IF search_string $ STR(EVAL(cfield))
         RETURN RGB(255,0,0) &&... lite red
      ENDIF

   CASE TYPE(cfield) = "D"
      IF search_string $ DTOC(EVAL(cfield))
         RETURN RGB(0,255,0) &&... lite red
      ENDIF

   OTHERWISE
      *  nop.  Not pretty, but I got lazy

ENDCASE
When you type something in the text box then setfocus to the grid, the
cells which contain the text, numbers or even date in the text box will
shine. Notice that it also hilights the memo fields which contain the
search text. I have used an inefficient substring match, and I'm sure
it would be quite a dog on a large table, but it could still be useful.

7- Complete form code
Here's the complete code. It also uses the FOXUSER.DBF, but you can use
another data source if you like:
Code:
***************************************************************
PUBLIC MultiColorGrid
WAIT WINDOW 'Creating test data...' NOWAIT 
SELECT * FROM SYS(2005) ;
   INTO TABLE testtest
   
REPLACE ALL type WITH 'INFO' FOR RECNO() % 3 = 0
REPLACE ALL type WITH 'JOE'  FOR RECNO() % 4 = 0
REPLACE ALL type WITH 'FRED' FOR RECNO() % 5 = 0
GO TOP

MultiColorGrid = CREATEOBJECT("MultiColorGrid")
MultiColorGrid.Show
RETURN

DEFINE CLASS MultiColorGrid AS form


   Top = 0
   Left = 1
   Height = 274
   Width = 500
   DoCreate = .T.
   Caption = "MultiColorGrid Demo"
   Name = "Multicolorgrid"


   ADD OBJECT grid1 AS grid WITH ;
      Height = 228, ;
      Left = 12, ;
      RecordSource = "testtest", ;
      Top = 24, ;
      Width = 490, ;
      Name = "Grid1"


   PROCEDURE typefontbold
      PARAMETERS search_string

      IF VARTYPE(search_string) # 'C'
         RETURN .F.
      ENDIF

      DO CASE 
         CASE 'PRE' $ (search_string)
            RETURN .T.
            
         CASE 'INFO' $ (search_string)
            RETURN .T.
            
         OTHERWISE
            RETURN .F.

      ENDCASE 
   ENDPROC


   PROCEDURE typefontitalic
      PARAMETERS search_string

      IF VARTYPE(search_string) # 'C'
         RETURN .F.
      ENDIF

      DO CASE 
         CASE 'PRE' $ (search_string)
            RETURN .T.
            
         CASE 'INFO' $ (search_string)
            RETURN .T.
            
         OTHERWISE
            RETURN .F.

      ENDCASE 
   ENDPROC


   PROCEDURE typebackcolor
      *FUNCTION BackTypeColor
      PARAMETERS search_string

      DO CASE
         CASE search_string = 'PREFW'
            RETURN RGB(255,0,0) &&... lite red

         CASE search_string = 'INFO'
            RETURN RGB(0,255,255) &&... lite blue

         CASE search_string = 'JOE'
            RETURN RGB(255,0,255) &&... fucia

         CASE search_string = 'FRED'
            RETURN RGB(0,255,0) &&... lite green

      ENDCASE
   ENDPROC


   PROCEDURE typeforecolor
      *FUNCTION ForeTypeColor
      PARAMETERS search_string

      DO CASE
         CASE search_string = 'PREFW'
            RETURN RGB(0,0,0) &&...black

         CASE search_string = 'INFO'
            RETURN RGB(0,0,0) &&...black

         CASE search_string = 'JOE'
            RETURN RGB(0,0,0) &&...black

         CASE search_string = 'FRED'
            RETURN RGB(0,0,0) &&...black

      ENDCASE
   ENDPROC


   PROCEDURE idforecolor

      *... Form method IDForeColor ...*
      PARAMETERS search_string

      DO CASE
         CASE search_string = 'PREFW'
            IF 'WIN' $ ID
               RETURN RGB(0,0,0) &&...black
            ELSE
               RETURN RGB(255,255,255) &&...white
            ENDIF

         CASE search_string = 'INFO'
            RETURN RGB(0,0,0) &&...black

         CASE search_string = 'JOE'
            IF 'WIN' $ ID
               RETURN RGB(0,0,0) &&...black
            ELSE
               RETURN RGB(255,255,255) &&... white
            ENDIF

         CASE search_string = 'FRED'
            IF 'WIN' $ ID
               RETURN RGB(0,0,0) &&...black
            ELSE
               RETURN RGB(255,255,255) &&... white
            ENDIF

      ENDCASE
      *... end IDForeColor...*
   ENDPROC


   PROCEDURE idbackcolor
      *... Form method IDBackColor...*
      PARAMETERS search_string

      DO CASE
         CASE search_string = 'PREFW'
            IF 'WIN' $ ID
               RETURN RGB(0,255,255) &&... lite blue
            ELSE
               RETURN RGB(0,0,128) &&... dark blue
            ENDIF

         CASE search_string = 'INFO'
            IF 'WIN' $ ID
               RETURN RGB(255,0,255) &&... fucia
            ELSE
               RETURN RGB(255,255,0) &&... yellow
            ENDIF

         CASE search_string = 'JOE'
            IF 'WIN' $ ID
               RETURN RGB(0,255,0) &&... lite green
            ELSE
               RETURN RGB(0,128,0) &&... dark green
            ENDIF

         CASE search_string = 'FRED'
            IF 'WIN' $ ID
               RETURN RGB(255,0,0) &&... lite red
            ELSE
               RETURN RGB(128,0,0) &&... dark red
            ENDIF

      ENDCASE
      *... end IDBackColor...*
   ENDPROC


   PROCEDURE grid1.Init

      This.column5.dynamicbackcolor = ;
         "IIF(MOD(recno(), 2) = 0, RGB(125,125,125), RGB(255,255,255))"

      This.column1.dynamicfontbold = ;
         "ThisForm.TypeFontBold(testtest.type)"

      This.column3.dynamicfontbold = ;
         "!EMPTY(testtest.name)"

      This.column1.dynamicfontItalic = ;
         "ThisForm.TypeFontItalic(testtest.type)"

      This.column1.dynamicbackcolor = ;
         "ThisForm.TypeBackColor(testtest.type)"

      This.column1.dynamicforecolor = ;
         "ThisForm.TypeForeColor(testtest.type)"
         
      This.column2.dynamicbackcolor = ;
         "ThisForm.IDBackColor(testtest.type)"

      This.column2.dynamicforecolor = ;
         "ThisForm.IDForeColor(testtest.type)"
   ENDPROC


ENDDEFINE
*
*-- EndDefine: MultiColorGrid
**************************************************
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top