Be seen. Boost your question’s priority for more expert views and faster solutions
Private mlngOldTopRow As Long
Then add code similar to this:Private Sub DataGrid1_Scroll(Cancel As Integer)
mlngOldTopRow = DataGrid1.FirstRow
End Sub
And when you want to go backDataGrid1.Scroll 0, mlngOldTopRow * -1
Private Sub DataGrid1_Scroll(Cancel As Integer)
mlngOldTopRow = DataGrid1.FirstRow
End Sub
and that is that if the user clicks the scrollbar down-arrow twice, this code will overwrite the FirstRow of the first click with the FirstRow of the second click, and so when you do DataGrid1.Scroll 0, mlngOldTopRow * -1 you will be taken back to the second FirstRow and not the first.we noticed that when double-clicking after the first 10 visible row, the value is 2 and if we click the 15 data row, the value is 6.Yes, it returns the number of the first visible row (BTW that's why I called the variable mlngOldTopRow. In any case in the DblClick or Click events mlngOldTopRow = DataGrid1.Row + DataGrid1.FirstRow will give the row number clicked. Note: The grid is zero-based but FirstRow is one-based, so, for example, double clicking the first row will give 0 + 1 = 1.
Private Sub DataGrid1_DblClick()
mlngOldTopRow = DataGrid1.Row + DataGrid1.FirstRow
End Sub
When you want to go back thenDataGrid1.Scroll 0, (DataGrid1.FirstRow - mlngOldTopRow) * -1
Public gcolRows As New Collection
Private Sub DataGrid1_DblClick()
gcolRows.Add DataGrid1.Row + DataGrid1.FirstRow
End Sub
Add this code in the place where you want to go back. For example if you've double-clicked rows 2, 4, and 11 and then you select row 15, this code when executed will take you in turn to rows 11, 4 and 2. In my testing I have it in a command button,If DataGrid1.Row = -1 Then
' None of the displayed rows have been selected so
' pretend that the first displayed row was selected
DataGrid1.Row = 1
End If
If gcolRows.Count > 0 Then
' Use the last entry in the collection to get us where we
' want to go
grs.Move gcolRows(gcolRows.Count) - DataGrid1.Row - DataGrid1.FirstRow
' Highlight the row to which we just moved
DataGrid1.MarqueeStyle = dbgHighlightRow
DataGrid1.SetFocus
SendKeys "{ENTER}"
' Remove the most recent entry in the collection
' since we've already used it.
gcolRows.Remove gcolRows.Count
Else
MsgBox "You are back where you started"
End If
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Using the Scroll method and the Firstrow property.