Grids to remember their last formatting  
Author Message
Aleniko29139





PostPosted: Visual FoxPro General, Grids to remember their last formatting Top

Hi everyone;

Before I start writing code for the grids to remember their last formatting per user - is there a setting I don't know of which will do that Simillar to Browse Last in 2.6

Thx.


Visual FoxPro1  
 
 
MarciaAkins





PostPosted: Visual FoxPro General, Grids to remember their last formatting Top

 
Hi everyone;
Before I start writing code for the grids to remember their last formatting per user - is there a setting I don't know of which will do that
Thx.

Here is your solution:

Create a table called grid_prefrence with this structure:

grd_id I
user_id I
path_name C(250)
column_name C(50)
column_order I
column_width I

Add these properties to your grid class:

*-- Set when the grid is instantiated if a
*-- configuration was found for it in the grid_preference table
lpreferencesexist = .T.
*-- An array to store the grid's default configuration
DIMENSION aprops[1,3]

Then you can add this to your grid class's init():

*** Get the grid preferences for this grid
*** and restore the grid configuration
IF THIS.COLUMNCOUNT > 0
 *** Save settings at grid instantiation
 THIS.SaveDefaultSettings()
 *** Now go get any previous settings that were saved
 THIS.RestoreGridSettings()
ENDIF

This code in SaveDefaultSettings():

*-- Save the grid properties when it is instantiated
LOCAL lnCol
WITH THIS
 DIMENSION .aprops[ .ColumnCount, 3 ]
 FOR lnCol = 1 TO .COLUMNCOUNT
  .aprops[ lnCol, 1 ] = .COLUMNS[ lnCol ].NAME
  .aprops[ lnCol, 2 ] = .COLUMNS[ lnCol ].COLUMNORDER
  .aprops[ lnCol, 3 ] = .COLUMNS[ lnCol ].WIDTH
 ENDFOR
ENDWITH

This code in RestoreGridSettings():

 *-- Configure the grid using the information saved in the Grd_Prefrence table
 LOCAL lcSQL, loCol, lnHandle
 lcSQL = [SELECT column_name, column_order, column_width FROM grid_preference WHERE path_name = ']
 lcSQL = lcSQL + SYS( 1272, THIS ) + [' AND user_id = ] + ;
  IIF( VARTYPE( oApp ) = 'O', TRANSFORM( oApp.sys_user_id ), [-1] ) + [ INTO CURSOR qTmp]
 &lcSQL

 *** See if we got comething - if we didn't, this is the first time
 *** that this form has been run - we have no preferences to restore
 IF RECCOUNT( [qTmp] ) > 0
  FOR EACH loCol IN THIS.COLUMNS
   SELECT qTmp
   LOCATE FOR column_name = loCol.NAME
   IF FOUND()
    loCol.COLUMNORDER = qTmp.column_order
    loCol.WIDTH = qTmp.column_width
   ENDIF
  ENDFOR
 ELSE
  THIS.lpreferencesexist = .F.
 ENDIF

And here is code to save the current setting to the grid_prefernece table when the grid is detroyed and to restore the deault setting on right click:

 *-- Called when the grid is destroyed to write the current configuration to the grid preference table
 PROCEDURE savegridsettings
 LOCAL lcSQL, loCol, lnHandle
 IF THIS.COLUMNCOUNT > 0
  *** if we do not yet have preferences saved for this grid and this user
  *** we need to insert a record, otherwise, we update
  FOR EACH loCol IN THIS.COLUMNS
   IF THIS.lpreferencesexist
    lcSQL = [UPDATE grid_preference SET column_order = ] + TRANSFORM( loCol.COLUMNORDER )
    lcSQL = lcSQL + [, column_width = ] + TRANSFORM( loCol.WIDTH )
    lcSQL = lcSQL + [ WHERE path_name = '] + SYS( 1272, THIS ) + [' AND column_name = ']
    lcSQL = lcSQL + loCol.NAME + [' AND user_id = ] + IIF( VARTYPE( oApp ) = 'O', TRANSFORM( oApp.sys_user_id ), [-1] )
   ELSE
    lcSQL = [INSERT INTO grid_preference ( user_id, path_name, column_name, column_order, column_width ) VALUES (]
    lcSQL = lcSQL + IIF( VARTYPE( oApp ) = 'O', TRANSFORM( oApp.sys_user_id ), [-1] ) + [, ']
    lcSQL = lcSQL + SYS( 1272, THIS ) + [', '] + loCol.NAME + [', ] + TRANSFORM( loCol.COLUMNORDER ) + [, ] + TRANSFORM( loCol.WIDTH ) + [ )]
   ENDIF
   &lcSQL
  ENDFOR
 ENDIF
 ENDPROC


 *-- Called from the right-click of the grid to restore the grid configuration to its default
 PROCEDURE showmenu
 LOCAL lnChoice

 *** If we do not have any columns, do nothing
 IF THIS.COLUMNCOUNT > 0
  *** Define the pop-up menu for resetting the grid to the default
  STORE 0 TO lnChoice
  DEFINE POPUP Reset2Default SHORTCUT RELATIVE FROM MROW(),MCOL()
  DEFINE BAR 1 OF Reset2Default PROMPT "Reset Grid Columns"
  ON SELECTION BAR 1 OF Reset2Default lnChoice = 1

  *** Activate the menu and process the result
  ACTIVATE POPUP Reset2Default
  IF lnChoice = 1
   *** We want to delete the preferences and reinitialize the grid
   THIS.RESETTODEFAULT()
  ENDIF
 ENDIF
 ENDPROC

 PROCEDURE DESTROY
 THIS.savegridsettings()
 ENDPROC


 PROCEDURE RESETTODEFAULT
 LPARAMETERS cProperty
 ***********************************************************************
 *** Changed By.: Marcia G. Akins on 27 August 2005
 *** Reason.....: Shanghai the native method to reset the grid columns
 *** ...........: to what they were when the form was instantiated
 ***********************************************************************
 LOCAL lnCol
 WITH THIS
  FOR lnCol = 1 TO .COLUMNCOUNT
   .COLUMNS[ lnCol ].COLUMNORDER = .aprops[ lnCol, 2 ]
   .COLUMNS[ lnCol ].WIDTH = .aprops[ lnCol, 3 ]
  ENDFOR
 ENDWITH
 NODEFAULT
 ENDPROC


 PROCEDURE RIGHTCLICK
 THIS.showmenu()
 ENDPROC


 
 
Aleniko





PostPosted: Visual FoxPro General, Grids to remember their last formatting Top

Marcia;

Now you're pampering me... I was prepared to write this myself... Its not interesting anymore :-)

Thanks so much!

Aleniko