# Tuesday, November 25, 2008

The Visual FoxPro Grid Control
I've often been heard to say that Visual FoxPro's grid is one of the greatest controls ever devised. I still feel that way, but wouldn't it be nice if some of the functionality that our customers want implemented in the grid came stock? You know, features like: sorting, filtering, incremental search, saving user preferences, and exporting to Excel. It'd be even cooler if this type of functionality could be implemented on any Visual FoxPro grid (regardless of the recordsource) by simply dropping a class on a form, setting a single property and writing a single line of code. Well, that's what I have set out to create with the class I present in this blog entry.

First Shown at Southwest Fox 2008
At Southwest Fox 2008, I presented a session entitled "Creating a Professional VFP Application from Start to Finish" in which I showed an earlier version of GridExtras. I've improved on it since then, so if you attended Southwest Fox 2008 you may want to get the control and sample from here rather than the session download. For those of you that didn't attend Southwest Fox 2008 (you should have - it was a blast!), you can see the basic functionality provided by GridExtras in the screenshots provided at the bottom of this blog entry.

How to Use the Class
I've provided a sample.exe application in the download so you can try it out and see how it's implemented. However, the basic steps are:

  • Drop an instance of the gridextras onto your form or container (one gridextras for each grid you want to enhance).
  • Set the GridExtras' GridExpression property to a string that will evaluate at runtime to the grid you are enhancing - "Thisform.Grid1" is the default value for this property.
  • Call the GridExtras' Setup() method whenever your grid is ready to go. I have no way of knowing if you are setting the recordsource at runtime, or whether you are adding columns in code, so this method is provided as a way for you to control when GridExtras will begin interacting with the grid.

That's all there is to getting GridExtras up and running for a grid in your application. As I say, I've included a sample in the download, so if you have any questions about how this is done just review the sample or you can post a comment here on my blog and I'll do my best to answer it.

Other Properties of Note
There are a few other GridExtras properties you may want to take note of and use, such as:

  • CompanyName and ProductName - these properties are used to determine where to save the user's grid preferences file which is profile isolated (C:\Users\Craig\AppData\Roaming\MyCompany\MyProduct\gridprefs.tmp on Vista or  C:\Documents and Settings\Craig\Application Data\MyCompany\MyProduct\gridprefs.tmp on Windows XP)
  • AllowGridExport, AllowGridFilter, AllowGridPreferences, AllowGridSort - these properties allow you to turn certain features of the class off or on depending on your needs.
  • TemplateTable - this property determines where GridExtras looks for the gridextras.dbf which is used to save the grid templates the user creates (user can save their filters and sorts to be able to easily recreate them at a later time). This property may need to be set if you plan to keep the gridextras table out on a network path such as with the shared database folder for your application.

What's Next
Just have fun with GridExtras and see if it's useful to you and your applications. In a perfect world, you'd find some way to improve it (add additional export formats, provide a column lock feature, etc.). If you do improve GridExtras, I'd appreciate it if you would contact me and share the improvements you've implemented. Thank you in advanced to those of you that decide to do so.

Until next time... Visual FoxPro Rocks!

Download GridExtras and Sample (approx. 170 KB)

Figure 1: GridExtras provides sorting, incremental search, and filtering capabilities to any Visual FoxPro grid.

 

Figure 2: GridExtras has additional features such as the ability to go semi-transparent and the ability to save the user's preferences (column order and width).

 

Figure 3: In addition to the features provided by GridExtras through a grid's column headers, there is also an icon added to the bottom right of the grid that allows the user to access the "Grid Templates and Export" screen seen in Figure 4.

 

Figure 4: The grid templates help the user save grid views so they can recreate them quickly in the future. The export feature of GridExtras can create XLS, XLSX, XLB, and XLM Excel formats. The grid's header captions are used to name the Excel columns when data is exported.

 

Tuesday, November 25, 2008 12:00:52 AM (GMT Standard Time, UTC+00:00)  #    Comments [12]
Tuesday, November 25, 2008 12:35:28 AM (GMT Standard Time, UTC+00:00)
Christmas at SPS!!!!!!! What a day... When it rains it pours! Thanks!!

Always looking for that "painless" grid that also encompasses alternating white and x color (like a ledger) but also features all of the above functionality (sort, search, etc.) built-in it.
Wednesday, November 26, 2008 12:40:51 AM (GMT Standard Time, UTC+00:00)
LOL! @ Christmas

More to come, I've got a ton of stuff lined up. Heating this blog back up to some of it's former glory. Stay tuned.

If you figure out a good way to implement a generic alternating color regardless of record position let me know. That would be a nice addition to this class. I've seen some implementations that use a relationship to a temporary cursor, but that seems a little extreme to me. There should be a way to do this using dynamicbackcolor or backstyle_access without introducing a bunch of stuff like cursors, indexes and relationships.
Tuesday, December 02, 2008 3:32:28 AM (GMT Standard Time, UTC+00:00)
This class is very fantasy.

I want to know, where can set the transparent % in the class. Because the cuurent % isn't easy to see when the mouse cursor left the filter form.

And the filter function is using, could add some icon or sign in the gird ? Beacuse user might forget the filter function which has been run.

Thanks your share.
Jurgen Chan
Tuesday, December 02, 2008 10:27:38 AM (GMT Standard Time, UTC+00:00)
Hi Jurgen,

To change the fade, go into the fadeform timer class in gridextras.vcx and change the Timer event to something like...

IF This.Parent._InForm
This.Parent._Fade = MIN(This.Parent._Fade + 60,255)
ELSE
This.Parent._Fade = MAX(This.Parent._Fade - 20,200)
ENDIF

_Sol_SetLayeredWindowAttributes(THISFORM.hWnd, 0, This.Parent._Fade, 2)

IF NOT BETWEEN(This.Parent._Fade,201,254)
This.Interval = 0
ENDIF

... note the change from 100 to 200 and the change from 101 to 201 that I made in the code. This would be a good thing for me to surface as a property on the gridextra class. Thank you for bringing the need to my attention.

As for a filter icon/sign... There is already one provided. Note the filter image (funnel) next to the left of "Customer ID" in the column header in Figure 3 above. The only time the filter image doesn't show is when the column is sorted. Only one picture can be in a header at a time so the sort image takes precedence. If you are not seeing the filter image check the headerfilterimage property of the gridextra class. It should be set to filter12.bmp.

If you are using gridextras.vcx in a project make sure you include all of the images that are provided as part of the gridextras source in your project. The compiler won't find them and include them for you. So you will have to add them manually to your project.
Sunday, December 07, 2008 2:16:31 PM (GMT Standard Time, UTC+00:00)
Hello Craig

I come to thank you for your formidable work
This kind of class is beautiful

I have just a problem with a grid in A form of first level
It me cree also a thisform.gridextra1.init vacuum but which pertube


It should then Be removed
I will look At so that the fenetre does not appear in the _Screen
I keep you informed

Thank you still for all your work
Excuse to me for my English

So long
JC

Tuesday, January 06, 2009 8:54:37 PM (GMT Standard Time, UTC+00:00)
Craig - I got this cool tool running in one of my apps! Thanks for this awesome piece of work.

May I ask if of the possibilities in the future of a "Clear ALL filters" button, and also some indication of whether the grid is currently filtered on each column.
Matt Slay
Tuesday, January 06, 2009 8:58:34 PM (GMT Standard Time, UTC+00:00)
Well, I see that once I added the GridExtrasProcs.prg to my procedure list, and added the path, now the icons show up for filtered columns. Good Job!
Matt Slay
Tuesday, January 06, 2009 9:05:22 PM (GMT Standard Time, UTC+00:00)
Sorting oddity... I noticed when sorting a column of strings, the strings that begin with numbers do not appear in the sorted list. I have values like: "1.250 WIDE BAR" AND "BOLTS, PLATED, 1/2 INCH", etc. all in the same column, and when I sort on that column, the strings that begin with digits do not show up. They reappear when I sort on a nother column.
Matt Slay
Thursday, January 08, 2009 6:35:36 PM (GMT Standard Time, UTC+00:00)
Craig,

I'm trying this class right now. Everything is OK, but I have some suggestions:

1) Search and Filter are part of the same window and single property AllowGridFilter ONs or OFFs both, Search and Filter. On RightClick() a was mostly interested in Search, but if Filter is off, Search is off, too;
2) Show Filter / Search window outside of filtered / searched column. There will be no need for playing with fading and the result would be always visible and uncluttered;
3) I also found myself mostly interested in filtering when I'm saving data: I see 100 records, I want to save only 20, but filter is not here;
4) This piece of code never worked for me, but if would work - most likely would produce error:
FUNCTION CopyToExcel()
CreateExcelTemplate(m.tcXLSFile)
IF !FILE(m.tcXLSFile)
m.lnReturn
ENDIF
5) Drop-down window fades down immediately after right click. I would consider this: right click, mouse in, mouse out, fading on.

Thank you,
Victor Savitskiy
Sunday, January 11, 2009 3:28:10 AM (GMT Standard Time, UTC+00:00)
Craig - You could have the Filter icon and the SortDirection icon both showing in the header if you combine them into one image file. So, your tool would need 5 images, and your code would have to be revised to determine which one to show in the header.

UpArrow.png
DownArrow.png
Filter.png
UpArrowWithFilter.png
DownArrowWithFilter.png

This would be a great addition to this really cool tool.
Matt Slay
Thursday, August 20, 2009 1:18:53 AM (GMT Daylight Time, UTC+01:00)
Great work!
Im using gridextras right now, everything works fine except when filtering dates. I can't get it to work with date columns! I´ve tried all SET DATE, SET EXACT AND SET CENTURY combinations, but it just doesn't filter dates. I'm using a cursor on the grid, not a regular table.

Any help will be appreciatted. Thanks.
Hector
Friday, November 06, 2009 11:17:41 AM (GMT Standard Time, UTC+00:00)
HI
Great work. I am using it and it is very useful. It is wonderful to look too. I have a small problem though.

I am using Gridextra in a tabel using a cursor. I also have start and end date which the user can select to filter the table contents. First time when everything is shown there is no problem. When i use the Grid extra functions (like sorting, filtering etc) and try to change the start or end date and refresh the data in the grid it gives a error message 'PROPERTY PICTURE IS NOT FOUND' and the trace window shows line 5 of setheaderimages coding under Gridextra1.

could you help me this?
thanks
GOWRI
All comments require the approval of the site owner before being displayed.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, sup, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview

 

Archive

<August 2010>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234