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:
SELECT * from SYS(2005) INTO TABLE testtest
Leave the table 'testtest' open and do these replaces, just to liven things up a little:
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:
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:
*... Form method TypeFontBold ...* PARAMETERS search_string
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:
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:
*... 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
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:
*************************************************************** 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