INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Grids

Synchronize Two Grids by craigsboyd
Posted: 4 Oct 03 (Edited 18 Feb 05)

Slighthaze = NULL



Though this is not an extremely practical solution as it stands, it does show in great detail how to position a grid on a particular column/row using many of the grid's methods, events and properties.  So if you've ever wondered how you can manipulate what columns/rows are shown in a grid through code this is for you.  Cut-N-Paste the code below into a prg file and run it from with VFP to see how it works.

CODE

PUBLIC oForm
oForm = CREATEOBJECT("clshookedgrids")
oForm.show()

DEFINE CLASS clshookedgrids AS form
    Top = 0
    Left = 0
    Height = 294
    Width = 620
    DoCreate = .T.
    Caption = "Form1"
    Name = "Form1"

    ADD OBJECT label1 as label WITH ;
        Caption = "Move around in this grid and watch what happens in the second grid below", ;
        AutoSize = .T., ;
        Left = 25, ;
        Top = 10, ;
        ForeColor = RGB(0,0,255), ;
        Name = "Label1"

    ADD OBJECT grid1 AS grid WITH ;
        ColumnCount = 21, ;
        Height = 108, ;
        Left = 25, ;
        RecordSource = "crsOne", ;
        Top = 27, ;
        Width = 569, ;
        Name = "Grid1"

    ADD OBJECT grid2 AS grid WITH ;
        ColumnCount = 21, ;
        Enabled = .T., ;
        Height = 108, ;
        Left = 25, ;
        RecordSource = "crsTwo", ;
        Top = 159, ;
        Width = 569, ;
        Name = "Grid2"

    PROCEDURE Load
        *!* Need some fake data to test this

        CREATE CURSOR crsOne(field1 c(30), field2 c(30), field3 c(30), field4 c(30), field5 c(30), ;
                        field6 c(30), field7 c(30), field8 c(30), field9 c(30), ;
                        field10 c(30), field11 c(30), field12 c(30), field13 c(30), field14 c(30), field15 c(30), ;
                        field16 c(30), field17 c(30), field18 c(30), field19 c(30), ;
                        field20 c(30), field21 c(30))

        FOR i = 1 TO 60
            INSERT INTO crsOne (field1, field2, field3, field4, field5, ;
                            field6, field7, field8, field9, ;
                            field10, field11, field12, field13, field14, field15, ;
                            field16, field17, field18, field19, field20, field21) ;
                    VALUES (RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), ;
                            RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), ;
                            RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), ;
                            RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), ;
                            RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), ;
                            RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), ;
                            RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3), RIGHT(SYS(2015),3)+RIGHT(SYS(2015),3))

        ENDFOR

        GO TOP IN "crsOne"

        SELECT * from crsOne INTO CURSOR crsTwo
    ENDPROC

    PROCEDURE Init
        *!* Because I am too lazy to recaption the headers
        FOR i = 1 TO 21
            thisform.grid1.Columns(i).header1.Caption = "FIELD " + ALLTRIM(STR(i))
            thisform.grid2.Columns(i).header1.Caption = "FIELD " + ALLTRIM(STR(i))
        ENDFOR
    ENDPROC

    PROCEDURE grid1.AfterRowColChange
        LPARAMETERS nColIndex
        DODEFAULT(nColIndex)
        thisform.LockScreen = .t.
        LOCAL nDirection, nRecno, nLeftColumn, nRelativeRow

        IF RECNO(this.RecordSource) != RECNO(THISFORM.GRID2.RecordSource)
            nRecno = RECNO(this.RecordSource)
            GOTO nRecno IN (THISFORM.GRID2.RecordSource)
        ENDIF
        nRelativeRow = this.RelativeRow
        thisform.grid2.enabled = .t.
        thisform.grid2.setfocus()
        IF nRelativeRow != thisform.grid2.RelativeRow
            IF nRelativeRow < THISFORM.GRID2.RelativeRow
                nDirection = 1
            ELSE
                nDirection = 0
            ENDIF
            FOR i = 1 TO 1000
                THISFORM.GRID2.doscroll(nDirection)
                IF nRelativeRow = THISFORM.GRID2.RelativeRow
                    EXIT
                ENDIF
            ENDFOR
        ENDIF
        thisform.grid2.enabled = .f.
        THISFORM.GRID2.refresh()
        this.setfocus()
        nLeftColumn = THIS.LeftColumn
        IF nLeftColumn != THISFORM.GRID2.LeftColumn
            IF nLeftColumn < THISFORM.GRID2.LeftColumn
                nDirection = 4
            ELSE
                nDirection = 5
            ENDIF
            FOR i = 1 TO 1000
                THISFORM.GRID2.doscroll(nDirection)
                IF nLeftColumn = THISFORM.GRID2.LeftColumn
                    EXIT
                ENDIF
            ENDFOR
        ENDIF
        thisform.LockScreen = .f.
    ENDPROC

    PROCEDURE grid1.Scrolled
        LPARAMETERS nDirection
        DODEFAULT(nDirection)
        thisform.grid2.doscroll(nDirection)
    ENDPROC

ENDDEFINE

Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close