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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use multi background colors in a grid.

Status
Not open for further replies.

ross6720

Programmer
May 3, 2004
15
US
I have a grid that contains data in 3 distinct time periods that I would like to display with a different background color for each time period. My form is not large enough to use 3 grids, so the grid I am using has to be scrollable vertically with the background colors following the rows when the grid is scrolled up or down.

Any suggestions would be appreciated.

 
Hi ross6720,

I haven't tried it but does faq184-624 do what you need?

Regards,

Mike
 

Thanks, Mike, for your help. This FAQ did, in fact, give me the capability I was looking for. It does work!

Ray Ross

 
Hi Ray,

You're welcome. Thank ChrisRChamberlain (author) also if you get the chance.

Regards,

Mike
 
ross6720,

While the FAQ is good, it would seem to me that you could just as well use the dynamicbackcolor of the column to achieve the results you are looking for. Either by adding a field to your grid's record source that holds a flagging value such as 1, 2, or 3... or if there will always be exactly 3 records for each of the time periods and the order is always correct you could use the MOD() function.

Here's some examples I'm thinking would work:

If you had an extra field called fldPeriod then a single line of code get's you what you want...
Code:
thisform.grid1.setall("DynamicBackColor", "IIF(MyData.fldPeriod = 1, RGB(255,0,0), IIF(MyData.fldPeriod = 2, RGB(0,255,0), RGB(0,0,255)))", "Column")

*!* You can use ICASE to further simplify in VFP 9
thisform.grid1.setall("DynamicBackColor", "ICASE(MyData.fldPeriod = 1, RGB(255,0,0), MyData.fldPeriod = 2, RGB(0,255,0), RGB(0,0,255))", "Column")

Now, let's say that you do indeed have 3 records for each period (and you aren't displaying a filter or sort of the record that would be different from their physical order - there are some drawbacks and gotchas with this one... the first one is safer, but this gives you another idea), but hear again it is a single line of code and you don't even need that extra field for this one...
Code:
thisform.grid1.setall("DynamicBackColor", "IIF(MOD(RECNO([MyData]),3) = 0, RGB(255,0,0), IIF(MOD(RECNO([MyData]), 2) = 0, RGB(0,255,0), RGB(0,0,255)))", "Column")

*!* You can use ICASE to further simplify in VFP 9
thisform.grid1.setall("DynamicBackColor", "ICASE(MOD(RECNO([MyData]),3) = 0,RGB(255,0,0), MOD(RECNO([MyData]),2) = 0,RGB(0,255,0), RGB(0,0,255))", "Column")

Be advised that I didn't test the code above, I just typed it in the Tek-Tips message window... so there could be some mistakes in what I just typed. It also assumes that you have a grid1 on a form and that the recordsource for that grid is either a table or cursor named MyData.

boyd.gif

 

ross6720,

I agree with Craig's overall approach (as usual).

Just to add one small point: If you want to avoid a complex nested IIF(), you can always write a function that accepts the period number as a parameter, and returns the corresponding colour code. You can then plug that function into the DynamicBakcColor property.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top