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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to order a column of data in a Grid

Status
Not open for further replies.

SitesMasstec

Programmer
Sep 26, 2010
508
Brasil
Hello colleagues!

I created a Grid on a form with data from a table. It has 7 columns.

Sometimes I will need to order ascending numerical or alphabetical columns.

When I click on the Header of the 4th column (Agente), nothing happens, in spite of the fact thar I have the following code in the Grid, Column4, Header1, Click event:
Code:
SET ORDER TO NOMAGENTE
(NOMAGENTE is the name of the field in the 4th column of the Grid).

GridOrdenacao.jpg
 
Sorry!
Solved.
thisform.Refresh at the end of the event command (SET ORDER TO...)
 
I have refrained from programming code in a Grid.Header. Instead, I use the function available in the Grid
Code:
Grid.GridHitTest(nXCoord_In, nYCoord_In[, nWhere_Out [, nRelRow_Out [, nRelCol_Out [, nView_Out]]]])
The only thing I still use in a header is the TAG and COMMENT properties, which are set in the design or init. The TAG property gets the INDEX TAG NAME that is required for the desired sorting.
With a "MouseDown/Up" event, I can then selectively prepare or evaluate the upcoming click/rightclick.
It looks something like this.
Code:
** MouseDown-Event
LPARAMETERS nButton, nShift, nXCoord, nYCoord

LOCAL Lc_PosClik, Lc_rRow, Lc_rCol, Lc_Obj

WITH This
    .Ms_Xpos = nXCoord
    .Ms_Ypos = nYCoord
    .Ms_HitC = .RelativeColumn
    .Ms_HitR = .RelativeRow

    .GridHitTest(.Ms_Xpos, .Ms_Ypos, @Lc_PosClik, @Lc_rRow, @Lc_rCol)

    FOR EACH Lc_Obj IN .Columns
        IF PEMSTATUS(Lc_Obj, "ColumnOrder", 5) ;
        .and. Lc_Obj.ColumnOrder == .Ms_RelC
            .Ms_HitO = Lc_Obj
            EXIT
        ENDIF
    ENDFOR

    IF nButton == 1 ;                                && Left-Click
    .and. .Ms_HitP == 1 ;                            && on Header
    .and. !EMPTY(.Ms_HitO.Header1.Tag) ;            && Index-Tag ?
    .and. USED(.RecordSource)
        RAISEEVENT(This, "GRID_SetOrder", .Ms_HitO.Header1.Tag)
    ENDIF

ENDWITH
**------------------------------------------
PROCEDURE Grid.GRID_SetOrder
LPARAMETERS Tc_Sort as String 

LOCAL Lc_Posi

ThisForm.LockScreen = .t.
WITH This
    IF VARTYPE(Tc_Sort) # "C" ;
    .or. EMPTY(Tc_Sort)
        Tc_Sort = ""
    ENDIF

    SELECT(.RecordSource)
    Lc_Posi = RECNO(.RecordSource)
    IF ORDER(.RecordSource) == Tc_Sort
        IF DESCENDING()
            SET ORDER TO Tc_Sort IN (.RecordSource)
        ELSE
            SET ORDER TO Tc_Sort IN (.RecordSource) DESCENDING 
        ENDIF
    ELSE 
        SET ORDER TO Tc_Sort IN (.RecordSource)
    ENDIF 

    IF ThisForm.Visible 
        GO TOP IN (.RecordSource)
        .Refresh()
        SET NEAR ON
        LOCATE FOR RECNO() == Lc_Posi
        SET NEAR OFF
        IF !FOUND(.RecordSource)
            GO TOP IN (.RecordSource)
        ENDIF
        .SetFocus()
    ENDIF

    FOR EACH Lo_Obj IN .Objects 
        IF PEMSTATUS(Lo_Obj,"Header1", 5) ;
        .and. GETWORDNUM(Lo_Obj.Header1.Tag, 1) == ORDER(.RecordSource)
            Lo_Obj.Header1.BackColor = orange
        ELSE
            Lo_Obj.Header1.BackColor = Lo_Obj.Header1.DefBackColor
        ENDIF
    ENDFOR
ENDWITH
ThisForm.LockScreen = .f.
The SetOrder-Procedure also might triggered by other sources, not only grid.header.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top