6 Replies - 2639 Views - Last Post: 29 June 2013 - 09:20 PM Rate Topic: -----

#1 dbellerue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-March 13

VB6 DataGrid Bookmark Error

Posted 24 June 2013 - 02:54 PM

When I scroll down a DataGrid, then select a row in the DataGrid, then rebind the DataGrid, the DataGrid displays the rows starting with the very first row, DataGrid.Row = 0.

How do I reposition the DataGrid to display the row that was last selected?

The DataGrid is bound to a Command in the DataEnvironment.

I have tried the following code in the DataGrid.SelChange event, using the BookMarks property, where lastRow is declared as a Variant:

[DataGrid1.Col = 0]
[lastRow = DataGrid1.Text]
[i = DataGrid1.Row]
[DataGrid1.Bookmark(i) = lastRow]

When I run the code I get an error on the last line: "Run-time error '13': Type mismatch"

Any ideas? Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: VB6 DataGrid Bookmark Error

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3720
  • View blog
  • Posts: 13,007
  • Joined: 12-December 12

Re: VB6 DataGrid Bookmark Error

Posted 24 June 2013 - 04:14 PM

lastRow = DataGrid1.Text

You are setting the lastRow to Text, which you cannot assign as a Bookmark.

lastRow = DataGrid1.Bookmark    'the current row
i = DataGrid1.Row
' ... later
DataGrid1.Bookmark(i) = lastRow

MSDN Bookmark Property

Unfortunately, I cannot test this, so it may not be 100% correct ;)

However, as you are re-binding the DataGrid I am also not sure whether the Bookmark will persist. It may be necessary to get the id of the current row (assuming one is present in the row) then navigate to this row by searching for this value. I don't know if it is possible to just get the current row number(?); even so, this is less reliable than using an id.

[If Bob were around he would be able to clarify this for us :) ]

This post has been edited by andrewsw: 24 June 2013 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3720
  • View blog
  • Posts: 13,007
  • Joined: 12-December 12

Re: VB6 DataGrid Bookmark Error

Posted 24 June 2013 - 04:37 PM

A little digging has uncovered these two lines:

rowIndex = DataGrid1.CurrentCell.RowNumber
DataGrid1.Scroll colIndex, rowIndex

but where I found the first line didn't make it clear whether this was for VB6.
Was This Post Helpful? 0
  • +
  • -

#4 dbellerue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-March 13

Re: VB6 DataGrid Bookmark Error

Posted 24 June 2013 - 05:21 PM

Thank you so much! With your help I came up with this solution:

Solution: After the DataGrid.Rebind, the DataGrid re-displays, with the last selected row at the top.

'in DataGrid1_SelChange:
'right after On Error Goto HandleError
lastRow = DataGrid1.Bookmark - 1

'in any sub:
'when DataGrid.Rebind occurs:
DataGrid1.ReBind
DataGrid1.Scroll 0, lastRow


Thanks again!

This post has been edited by andrewsw: 24 June 2013 - 05:30 PM
Reason for edit:: Removed square brackets

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3720
  • View blog
  • Posts: 13,007
  • Joined: 12-December 12

Re: VB6 DataGrid Bookmark Error

Posted 24 June 2013 - 05:39 PM

Glad you got it sorted.

However, I don't think you should be performing arithmetic on a Bookmark (subtracting 1). A Bookmark is for internal use and offers no guarantees about its specific value. Unfortunately, I'm not able to offer an alternative currently (other than the information I provided in previous posts).

BTW What's with all the square brackets? I've removed them from your most recent post. When creating a post, highlight your code and click the [ CODE ] button just above where you are typing. Press the Preview Post button until it is correct.

Andy.

This post has been edited by andrewsw: 24 June 2013 - 05:40 PM

Was This Post Helpful? 0
  • +
  • -

#6 dbellerue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-March 13

Re: VB6 DataGrid Bookmark Error

Posted 25 June 2013 - 04:18 AM

View Postandrewsw, on 24 June 2013 - 05:39 PM, said:

Glad you got it sorted.

However, I don't think you should be performing arithmetic on a Bookmark (subtracting 1). A Bookmark is for internal use and offers no guarantees about its specific value. Unfortunately, I'm not able to offer an alternative currently (other than the information I provided in previous posts).

BTW What's with all the square brackets? I've removed them from your most recent post. When creating a post, highlight your code and click the [ CODE ] button just above where you are typing. Press the Preview Post button until it is correct.

Andy.


I guess I'm a newbie. The reply background says "Put Tags Around Your Code: [code]..."

I'll try declaring lastRow as an Integer.

Thanks again.
Was This Post Helpful? 0
  • +
  • -

#7 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: VB6 DataGrid Bookmark Error

Posted 29 June 2013 - 09:20 PM

Bookmark property is a Variant, so neither text nor integer will work. This works (an ado data control, the datagrid, two command buttons on the form):
Dim x

Private Sub Command1_Click()
x = dg.Bookmark
End Sub

Private Sub Command2_Click()
dg.ReBind
dg.Bookmark = x
End Sub

I just used an ADO Data Control tied to NWIND.mdb, using "Select * from Customers" as the RecordSource. On the grid, go to any record. Click command1. Go to any other record. Click command2. The datagrid will reposition to where it was when you clicked command1.

This post has been edited by BobRodes: 29 June 2013 - 09:33 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1