Visual FoxPro’s Grid
It is my opinion that the Visual FoxPro grid has gotten a bum rap at times. Though it does possess it’s share of eccentricities, it is probably one of the most powerful and useful controls I have ever had the pleasure of working with. The things that it is capable of doing are simply amazing. Heck, you can have multiple controls in a column or even put a grid inside a grid, so you essentially have a row/column intersect (cell) representing many records.

In this entry, I want to show you a little known feature of the grid control. If you know about this feature, then you are one of a handful. When the grid is refreshing or otherwise repainting it will access the backstyle for every control the columns contain, and anything you do in there will stick for the individual controls shown in the grid.

Some help from the MS Fox Team
Allow me to try and clarify… let’s say you have a grid with a single column and that column contains Textbox1. If you set the backcolor of the Textbox1 through code to, let’s say, the color Red, then every cell shown in column1 will turn red. So, how to get dynamic colors (individual cells colored differently within the same column)? Well, the MS Fox Team provides us with some dynamic properties of the column object that will act on individual cells (DynamicAlignment, DynamicBackColor, DynamicCurrentControl, DynamicFontBold, DynamicFontItalic, DynamicFontName, DynamicFontOutline, DynamicFontShadow, DynamicFontSize, DynamicFontStrikeThru, DynamicFontUnderline, DynamicForeColor, and DynamicInputMask). These are all very useful, and in the scenario I provided the DynamicBackColor will work nicely to change the backcolor of individual cells within a column of the grid. But what if you wanted to do something more complex? What if you had a container in the column and that container contained multiple objects and you wanted to set their forecolors and backcolors to completely different colors dynamically or what if you wanted to display different pictures within the grid cells?

Developers’ usual and ingenious solutions
One approach to these problems is to use multiple controls within the column of the grid and then use DynamicCurrentControl to decide which to display. Such as one textbox that has a red background and another that has a white background, or different image controls all set to different pictures.

Another ingenious method some Visual FoxPro developers imploy when solving this problem is to subclass the column object and then “hook” into a non-used dynamic property of the column to use it for other purposes (Such as DynamicForeColor) If the ForeColor is being set to 1 then do this, if the forecolor is being set to 2 do something else, if the forecolor is 3… and so on. While this approach and the preceding one are both valid way of doing this, there is another way.

Another way
As previously noted, the backstyle is accessed for the currentcontrol in a column, and it’s not accessed just once, it is accessed for every visible cell in the grid. So, using this we can do nearly whatever we want as far as dynamic formatting and display. Want different images displayed? Just subclass a container and put an image control within it and place it in the column. Then, in the backstyle_access method (you’ll need to add this access method to the subclassed container) set the value of the contained image’s picture property to a field in the recordsource that holds all the different paths in the image’s backstyle_access method.

this.picture = crsImages.Paths

There’s an example of doing this included in the download. When you run the example it will ask you for a directory containing images. If one is not readily available to you, you can select the folder the project is in because I have included the below screen shots in it. This solution is more efficient than having an image control in the column for each image you want displayed in the grid… 100 images = 100 image controls? I’d rather just deal with one.

Calendar class and real world example
OK, but what about something a little more real world? What kinds of things is being able to dynamically alter items within a container using the backstyle_access good for? All sorts of stuff. In order to demonstrate the just one useful example of this, I put together some quick calendar demos. I’ve created an advanced calendar class using the grid control. This should demonstrate well the power of utilizing backstyle_access in a grid, not to mention the power of Visual FoxPro. Below is the download link for the project source and some screen shots (so you can know what to expect). There is an executable provided in the source folder for running the examples, or if you prefer you can run the forms individually. The examples were created using Visual FoxPro 9.0, so if you’re using an earlier version you will have to modify the code or better yet, upgrade! There are more great things to come, Visual FoxPro Rocks!

Download VFP Calendar Examples and Source (91 KB approx.)

Just a simple calendar

Demonstrates some of the advanced display features of the calendar class

Improved Date and DateTime controls created using the calendar class