Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grid Header Sort Class

Status
Not open for further replies.

DChalom

Programmer
Jun 8, 2002
59
US
How would I go about creating a Grid Header Sort Class?

The grid i am using this on could be defined two seperate ways with two different sets of columns so I cannot predefine the headers click for a sort. I must create a Header Class and then associate that class with the header.

Does anyone have any experience doing this?






Dorian C. Chalom
 
Here's a previous posting that helped me a lot.
thread184-786296

Good Luck,
JRB-Bldr
 
You could download InGrid from the Universal Thread.

Craig Berntson
MCSD, Visual FoxPro MVP, Author, CrysDev: A Developer's Guide to Integrating Crystal Reports"
 
Hello Dorian.

You do not say which version of VFP you are using, but if you are using VFP 8, this is a piece of cake and you do not need a cusrom header class. You can do it using BINDEVENT() like so in the grid's init():

Code:
*** now make sure that the dblclick method of all the contained text boxes
*** delegate to the grid's dblclick()
FOR EACH loColumn IN This.Columns 
  FOR EACH loControl IN loColumn.Controls
    IF LOWER( loControl.BaseClass ) = 'header'
      BINDEVENT( loControl, 'Click', This, 'SortGrid' )
    ELSE
      IF PEMSTATUS( loControl, [dblClick], 5 )      
        BINDEVENT( loControl, 'dblClick', This, 'dblClick' )  
      ENDIF
    ENDIF
  ENDFOR
ENDFOR

This code in the grid's SortGrid method:
Code:
LOCAL laEvents[ 1 ], loHeader, lcField, loColumn, lcSortOrder, loControl
LOCAL llFoundColumn, llAllowCellSelection, lnRecNo

IF INLIST( Thisform.cEditMode, [ADD], [EDIT] )
  RETURN
ENDIF

*** First of all, see which column fired off this event
AEVENTS( laEvents, 0 )
loHeader = laEvents[ 1 ]
IF VARTYPE( loHeader ) = 'O'
  *** First See if a ControlsSource was set for the column
  WITH loHeader.Parent
    lcField = ''
    IF NOT EMPTY( .ControlSource )
      *** Cool. Use it to decide how to sort the grid
      IF NOT EMPTY( .ControlSource ) AND ( '.' $ .ControlSource ) AND NOT( '(' $ .ControlSource )
        lcField = JUSTEXT( .ControlSource )
      ENDIF
    ENDIF
  ENDWITH
  This.cSortField = []
  *** we have a field - let's see if it already has a sort order set
  *** if it does, it will have the appropriate picture in the header
  lcSortOrder = ''
  IF NOT EMPTY( loHeader.Picture )
    lcSortOrder = IIF( LOWER( JUSTFNAME( loHeader.Picture ) ) == 'down.bmp', '', 'DESC' )
  ELSE
    *** See if there is a visual cue on any of the other grid
    *** column headers and remove it if there is
    FOR EACH loColumn IN This.Columns
      FOR EACH loControl IN loColumn.Controls
        IF LOWER( loControl.BaseClass ) == [header]
          IF NOT EMPTY( loControl.Picture )
            llFoundColumn = .T.
            loControl.Picture = []
            loControl.FontBold = .F.
            EXIT
          ENDIF
        ENDIF
      ENDFOR
      IF llFoundColumn
        EXIT
      ENDIF
    ENDFOR
  ENDIF
  
  *** if we have a field - let's sort
  IF NOT EMPTY( lcField )
    *** There seems to be a refresh issue here
    *** because even though the data is in the cursor
    *** it is not showing up in the grid after the sort
    *** and it looks like it is related to AllowCellSelection being .F.
    llAllowCellSelection = This.AllowCellSelection
    This.AllowCellSelection = .F.
    This.Refresh()
    KEYBOARD '{CTRL+TAB}'
    
    *** Check to see if the tag exists assume
    *** that if there is a tag on this field, it has the same name as the field
    IF TAGNO( lcField, This.RecordSource, This.RecordSource )
      This.cSortField = lcField
      lnRecNo = RECNO( This.RecordSource )
      *** Go ahead and set the order for the table
      SELECT ( This.RecordSource )
      SET ORDER TO ( lcField )
      This.SetFocus()
      IF lnRecNo # 0
        GO lnRecNo IN ( This.RecordSource )
      ENDIF
      *** And set the visual cues on the header
      loHeader.Picture = IIF( EMPTY( lcSortOrder ), [..\graphics\up.bmp], [..\graphics\down.bmp] )
      loHeader.FontBold = .T.
      This.AllowCellSelection = llAllowCellSelection
    ENDIF  
  ENDIF
ENDIF


Marcia G. Akins
 
I am using VFP 6.0....

Dorian C. Chalom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top