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

How to freeze the lefmost column in a grid

Grids

How to freeze the lefmost column in a grid

by  Mike Gagnon  Posted    (Edited  )
This example shows how to freeze the first (to freeze more then one column the code complicates itself exponentially) column in a grid. You can see the effect by copying the following in a program and running it and scroll the grid to right, noticing that the first column is always visible. The important code is in the grid's AfterRowColChange.
Code:
Public oForm
oForm=Createobject("form1")
oForm.AddObject("grid1","grid1")
oForm.Show(1)
Define Class grid1 As Grid
    DeleteMark = .F.
    ColumnCount = 5
    PROCEDURE BeforeRowColChange
    Lparameters nColIndex
        thisform.lockscreen = .t.
    ENDPROC
    Procedure AfterRowColChange
    Lparameters nColIndex
    Local i
    With This
        If .Columns(1).ColumnOrder < .LeftColumn
            For i=.Columns(1).ColumnOrder To .LeftColumn-1
                .Columns(1).ColumnOrder = .Columns(1).ColumnOrder + 1
            Endfor
        Else
            For i=.LeftColumn+1 To .Columns(1).ColumnOrder
                .Columns(1).ColumnOrder = .Columns(1).ColumnOrder - 1
            Endfor
        ENDIF
        .Refresh()
    ENDWITH
    thisform.lockscreen = .f.
Endproc
    Procedure Init
    With This
        .Columns(1).BackColor = Rgb(255,0,0)
        For i = 1 To .ColumnCount
            .Columns(i).header1.Caption=Justext(.Columns(i).ControlSource) && Line will not work in VFP5.0
        Endfor
    Endwith
    This.RecordSource="myGridCursor"
Endproc
Visible = .T.
Enddefine
Define Class form1 As Form
    Procedure Init
    Create Cursor myGridCursor (date1 D,Name c(20),address c(20),phone c(12),zip c(10))
    Insert Into myGridCursor (date1,Name,address,phone,zip) Values (Date(),"Mike","123 nowhere","1-800-555-5555","123432")
    Insert Into myGridCursor (date1,Name,address,phone,zip) Values (Date()+1,"Paul","321 somewhere","1-800-555-6543","0978675")
    Insert Into myGridCursor (date1,Name,address,phone,zip) Values (Date()+3,"John","765 Here","1-800-555-3214","9587464")
    Go Top
Endproc
Enddefine

Special thanks to SlightHaze for the extra code to prevent the flickering effect.
Mike Gagnon

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top