Link to home
Start Free TrialLog in
Avatar of wenchang
wenchang

asked on

controlling dbgrid

hi..
just like to ask if there's any way to scroll 2 grid boxes simultaneously...i can get it to scroll down but not up..
Avatar of TheAnswerMan
TheAnswerMan

I assume you are using the same record.. or atleast the key in the tables are the same..

you can just do

sub Grid1_RowColChange
  On error resume next
  Grid2.bookmark = Grid1.Bookmark
end sub


actually with some grids.. you may have to use this to change the rows..

sub Grid1_RowColChange
  On error resume next
  Grid2.Row = Grid1.Row
end sub
Avatar of wenchang

ASKER

i afraid your suggestion cannot work....
i'm using 2 different tables for the two grids....

I am confused.. you can get what to scroll down but not up.

You have two different tables.. and two different grids..

what do you want to happen in each grid
i'll try to explain it more clearly.....
i have 2 grid boxes and 1 grid to display data from 1 table and the other grid to display data from another table..
if i use,
sub grid1_scroll()
   grid2.scroll 0, 1
end sub
i can get the 2 grids to scroll down together...but if i try to scroll up, grid2 will still be scrolling down....
hope it's more clear now...thanxx
Try
   grid2.scroll 0, -1
to scroll up
I realise this will not solve your whole problem
Add two timer1 to your form. Disable both timers (enabled = false) and set interval to 10.
Now add the following code.

Private Sub DBGrid1_Scroll(Cancel As Integer)
    Timer1.Enabled = True
End Sub

Private Sub DBGrid2_Scroll(Cancel As Integer)
    Timer2.Enabled = True
End Sub

Private Sub Timer1_Timer()
    Timer1.Enabled = False
    DBGrid2.FirstRow = DBGrid1.FirstRow
End Sub

Private Sub Timer2_Timer()
    Timer2.Enabled = False
    DBGrid1.FirstRow = DBGrid2.FirstRow
End Sub
oops...can't work....
How so, can't work?? I don't understand
the error message is "invalid bookmark"
what's the timer for anyway???
what's the timer for anyway???
It works with the biblio database
try it without the timer.
the scroll event is send before the scroll is executed. that means that firstrow still contains the old value. You need the new value. That what the timer does. It allows the value to be updated.
Just try my solution in a simple project.
Add a data control, two timers and two grid controls to a form.
Bind the grids to the data control and see that it works.

You can solve this by using VB's built in scrollbar.

Add 2 scrollbars to your form.
add the following code to your form:

Private Sub VScroll1_Change()
DBGrid1.Scroll 0, VScroll1.Value - VScroll2.Value
DBGrid2.Scroll 0, VScroll1.Value - VScroll2.Value
VScroll2.Value = VScroll1.Value

End Sub

Private Sub VScroll2_Change()
DBGrid1.Scroll 0, VScroll2.Value - VScroll1.Value
DBGrid2.Scroll 0, VScroll2.Value - VScroll1.Value
VScroll1.Value = VScroll2.Value

End Sub


You can then align the scrollbars to the right of your grids & 
turn off the internal scrollbar of the grids.

to have the scrollbar act more like the original you can also change the vScroll1.Max & the vScroll2.Max properties to equal the number of records in the grids.

I hope this works for you.
in the scroll event of one dbgrid, set the toprow properties of the two girds the same
here is an example

Private Sub dbGrid1_Scroll()
    dbGrid2.TopRow = dbGrid1.TopRow
End Sub

toprow is not a property of dbgrid. He probably means firstrow and firstrow is only set after the scroll event.
thanxx vbWhiz....
now how do i give u the points....
vbWhiz, how do you now when to update the max-values of the scrollbar and what if you scroll using the cursor keys or home and end.
wait a minute...i forgot to ask this..
i use rdoResultsets in my application and i sometimes gets this "maximun open cursors exceeded"...what does this means...

I don't know about that error message specifically but there is some good info on cursors in the help file (RDO cursors) & (CursorDriver) you can modify the cursor style RDO uses using CursorDriver.

If your question is answered I can post my comments as an answer.
ok....i'll go read up the help file...thanxx
think that's about it for this question.....u can post your comments as  an answer n i can give u the points..
I made that once with 4 listboxes scrolling togheter. can't
find the code now, but i remember that i had a problem, and finally - i disabled 3 of the 4, so the user could scroll
only with One scrollbar. but they all moved simulatly . if u want - i can look for the code in the old HardDisk, but have to
put him in the machine first..........
it's alright...vbWhiz has already solved my problem...thanxx anyway....
does it work in a situation when the user scroll in one
scrollbar and stop scrollimg whitout letting the mouse button off ? i suppose u have a mismach at that moment.
ASKER CERTIFIED SOLUTION
Avatar of vbWhiz
vbWhiz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try the following, this code is written assuming dbgrid1 will scroll responding to dbgrid2

'in the scroll event of dbgrid2, write the following code. This 'is to call or post the dbgrid1_scroll event, here we should 'post the dbgrid1 scroll event so that dbgrid2 is completely 'refreshed after the scroll
Private Sub DBGrid2_Scroll(Cancel As Integer)
     PostMessage DBGrid1.hwnd, 276, 0, 0
End Sub

'In the dbgrid1_scroll event write the following code
Private Sub DBGrid1_Scroll(Cancel As Integer)
    'set the row od dbgrid2 to 0
    DBGrid2.Row = 0
    'get the bookmark of the first row
    nextbookmark = DBGrid2.GetBookmark(DBGrid2.Row)
   
    'get number of rows in dbgrid1
    Data1.Recordset.MoveLast
    rows1 = Data1.Recordset.RecordCount
    Data1.Recordset.MoveFirst

    'get number of rows in dbgrid2
    Data2.Recordset.MoveLast
    rows2 = Data2.Recordset.RecordCount
    Data2.Recordset.MoveFirst
       
    'scroll to 1st row of dbgrid2
    For i = 1 To rows2
        DBGrid2.Scroll 0, -1
        DBGrid2.Row = 0
    Next
   
   'scroll to 1st row of dbgrid1
    For i = 1 To rows1
        DBGrid1.Scroll 0, -1
        DBGrid1.Row = 0
    Next
   
    ' for each row in dbgrid2 check if the current row is same   'as the stored bookmark, if not scroll both the grids
    For i = 1 To rows2
        DBGrid2.Row = 0
        If DBGrid2.GetBookmark(0) = nextbookmark Then
            Exit For
        Else
            DBGrid2.Scroll 0, 1
            DBGrid1.Scroll 0, 1
        End If
    Next
   
End Sub