|How to refresh grid properly after sorting|
I have seen many questions about sorting of a grid by clicking on its header and proper refreshing of grid after sorting. There is a reliable way to sort (index) grid cursor and properly refresh the grid without change of the record number AND with very good looking. I made this in my grid class and tested it already 2 years so far.
Note that the way I used has a requirement for the grid cursor - it should not contain changes or may contain, but only in table buffering mode. This is because the changes will be automatically updated when the record pointer moved for cursors in the row buffering mode. I prohibit any sorting until the user saves changes - a solution on the interface level.
Following is the code from my grid class commented and fixed to do not confuse by details of my framework:
* Used grid custom properties:
* lSorting - flag that indicates sorting refreshing, so all activity related with row
* change in grid should be disabled for the duration of this routine
* nRecno - stores record number of current row in grid. This property should be populated
* in the AfterRowColChange event by record number each time record number changed
* lInit - flag, should be .T. when Init method is running
* column reference should be passed of column being sorted
* called internally only as event from the column header after sorting
.lSorting = .T.
&& above flag used in the AfterRowColChange and other grid events to do not fire
&& any code during running of this method. This method might cause weird record
&& moving by grid when sorted, so anyway it is required. We will force record
&& moveng too for better refreshing.
local lcOldArea, lcGridAlias
m.lcOldArea = select(0)
m.lcGridAlias = this.RecordSource
* store 'real' grid position, this is needed because it might change in the
* next chapter
m.nOldRecNo = .nRecno && nRecNo - property that updated in AfterRowColChange event
&& of grid in AfterRowColChange event flag lSorting prevents
&& change of this property
&& restore row position - it might be changed during indexing and cell activation
&& after refresh after sorting you can see grid shows, for example, last row only,
&& when really it contains 3 rows and all fit into grid. This is a problem we going
&& to solve by following code lines
* this must be BEFORE any refresh of grid
LOCATE && FOR .T., same as 'go top', but works more quickly for
&& filtered data
&& activate cell that is appropriate to sorting column,
&& so grid will not scroll out from sorting column. When we do not do this, after
&& click on the header, grid will scroll to current cell (column) focused, that
&& might hide (scroll out) column that is just sorted - looks confusing.
if !.lInit && flag that is .T. when Init method is running.
&& during Grid Init, however, we should not do this because SetFocus will
&& cause error. Just refresh it. This is needed to check for case when we
&& want to restore grid sorting when form loads from user preferenses
DOEVENTS && this is required before any other actions to correctly refresh grid
&& and do not move record pointer weirdly
.lSorting = .F.
It is also recommended to use thisform.LockScreen to prevent the grid flashing - hide all grid refreshing from users, so they will see just the results.
When screen is locked, first Refresh commands are required just for repositioning current grid scrolling position. As a result, the work grid does for displaying (drawing) records gone for nothing. In cause you have really a lot of things placed in the Dynamic* properties of the grid columns to organize some fancy things, like highlighting of the current record or colour grid cells by different colours appropriate to some conditions in the data records, it will look quite slow when doing multiple refreshes like in this article. To speed it up, use very simple approach: Before first refreshes just after locking the screen, backup all Dynamic* settings of all columns in some array or additional properties, then clear them all. Just before the last refresh restore all Dynamic* property values. This speeds up the total time of above approach running.
More entries from this member
- 256-color icons in EXE application compiled by VFP May 28, 2002
VFP does not support 256-color icons as project's icon (icon displayed for EXE in Windows Explorer). But 16-color icons usually looks ugly and too simplified.
Here is an approach that allows organize 256-color icons for VFP EXE application.
The approach is based on use of Resource Hacker application. You can download it from following URL:
(UPDATED: original link does not work for some reason. Let me know if this one also does not ...
- Force positioning of text cursor by right mouse click in EditBox and RTF ActiveX controls February 8, 2002
There is a situation often when we need some way to position a text cursor in the text of Editbox or RTF ActiveX controlwhen right click on the control. For example, we want to display a shortcut menu on right click. In menu, user select an option that insert some text into the control on place of current position. However, right click on the control does not change the text cursor position, that confuse users. It would be good to right click, cursor moves to the clicked point and then menu disp...
- Grid reconstruction and how to avoid it March 16, 2001
Have you ever found a situation when your grid don't want to behave as you directed in design time? Custom controls in columns lost? Code of columns, headers or controls event not running? Read this FAQ then.
Grid reconstruction behavior is a complete removing of all grid's controls and columns and creating them again using default VFP controls. This cause lose of all methods, property settings and objects in columns and all column objects, column control to display and edit data is reset to ...
- How to assign code to event in run-time? September 21, 2001
In VFP there are no direct way to assign code to the method or event in run-time mode. However, some workaround is possible with use of additional, 'hook' class. The sample below describes the approach. It is possible to make it generic. The approach is very useful to catch, for example, events of the _Screen form.
- How to display Tool Tips for different parts of the Grid? February 1, 2002
In VFP6 Grid value of the ToolTipText property of controls is displayed only for the entire grid or for current control when mouse is over it. Becuase there are a lot of parts in the grid, it is often useful to display tool tip for different parts separately. Unfortunately, VFP interprets grid as a single control, displayed tool tip appearance for grid is not changed immediately even when ToolTipText property is changed, until mouse is moved out of the grid and returned into the grid into requir...
- How to freeze (lock) a column in the grid without use of split bar February 8, 2002
The approach is based on a simple principle. LeftColumn property of the grid represents a column that is currently the leftmost visible column in the grid with current horizontal scrolling. As we scroll the grid horizontally, this property changes. We can assign this number to the ColumnOrder property of the column we want to hold and that’s all:
this.Columns(1).ColumnOrder = this.LeftColumn
You can use this in the Scrolled and AfterRowColChange events of the grid. However, there is a...
- How to get form caption font properties October 6, 2000
Windows form caption properties stored in the user preferences. You can change them using Windows Control Panel\Display\Appearance. To get these parameters programmatically in VFP, you can use following routine as a sample.
- How to make a collection property of VFP object February 6, 2001
After some investigation, I managed to make a collection-like property of object. The following is a simple sample of how to organize collection-like access (rough, need to improve it to work as real collection). It is just to show an approach.
- How to make a grid appear like a list similar to list box June 20, 2001
This article describes how to configure grid to appear as a list similar to the list box or the drop-down list of combo box. It is for appearance only. All other things are as usual for any grid.
This is useful for case when need to replace list box by a grid to display more records, because listbox is slow when displaying 1000+ rows in listbox.
- How to make SQL Pass-Through cursor updatable April 9, 2002
The SQLEXEC() function in VFP allows downloading of data from any data source through ODBC. Unlike remote views, SQL Pass-Through approach does not provide default (automatic) settings in the returned cursor to update data on the server after changes in the cursor at the client side. Anyway, it is possible to customize required properties to organize updating.
First of all, it is recommended to read chapters about SQL Pass Through commands in the MSDN, specially, chapters that explain propert...
- How to organize SUM() for character field in SQL Server SELECT query September 4, 2001
There is a little problem for query to get something like a sum() for a character field to concatenate character values from all records in the group. Here is described solution.
The approach below have an limitation to max 30 records in group for concatenation. Test it if it will work for more records in group or there should be another limitation on your server, becaus ethis number is depended on SQL Server capabilities and length of the character field being concatenated.
- How to put grid on the form programmatically February 5, 2001
Suggest Grid1 is a name of grid required to put onto the form, grid should contain 10 columns an display data from the MyAlias alias. Grid should contain a combo box in the second colunm with the name Combo1. Following sample routine will allow you to see a generic idea of how to put grid on the form programmatically and recreate it again when needed. You can make it as a custom method of the form and call it when needed (for example, thisform.MakeGrid or thisform.RefreshGrid).
- How to work with Image field type on SQL Server (binary data)? January 11, 2001
Text field on SQL Serevr is not good to store binary data because it is not reliable and often SQL Server corrupts binary data in text field. varbinary type could be too short.
There are certain difficulties to use image field on SQL Server to store and read binary data in VFP. Image field on SQL Server mapped to general field in VFP by default, causing some difficulties when using SQL Pass Through functions. The workaround idea is to try to use memo field to update data on SQL server.
- SPT cursor fetching and progress displaying for SPT cursor downloading February 5, 2002
SQL Pass Through commands often can take a lot of time for running and returning the result set to the client. It is often usefult to show a progress bar in case of such lengthy process. Also, FetchAsNeeded option, available for views, could be very useful for SPT cursors as well. However, how we can open SPT cursor and set FetchAsNeeded option for it?
Here is a sample code for opening the cursor in "FetchAsNeeded" mode, as well as a description of approach for organizing the progress bar for...