Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How To Persist Grid column widths
Message
From
14/12/2005 15:32:06
Irv Adams
MSC Managed Care, Inc.
Florida, United States
 
 
To
09/12/2005 15:17:14
General information
Forum:
Visual FoxPro
Category:
Forms & Form designer
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01076375
Message ID:
01078022
Views:
24
Marcia,

Your code is excellent, I have incorporated this in a base class with a small change to create table if not found. Nice work!

Irv.

>Is there a simple means of persisting a grid's column widths? We have some columns whose data width can sometimes extend beyond the default width of the grid column. There is not enough room to display all columns in the grid though so if a user extends the width of column1 then column6 is no longer visible (without scrolling). Some users would like to see changes to grid column widths persist though. Is there a simple means of doing this? Where to store the information?
>
>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
>
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform