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!

Synchronize Two Grids

Grids

Synchronize Two Grids

by  craigsboyd  Posted    (Edited  )
Slighthaze = [color blue]NULL[/color]

[img http://www.sweetpotatosoftware.com/ttimages/synchgrid.gif]

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
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