dcsimg
Results 1 to 5 of 5
  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2017
    Posts
    3

    VB6 DataGrid, the grid row index vs the RecordSet row index

    Hi,

    When I change the sort or filter of the DataGrid's RecordSet, the DataGrid.Row numbers will not be sequential anymore in the grid.
    Means if I verify the first 5 rows in the grid, the .Row properties are 3,7,0,1,8 for example, based on the sort criteria.

    my question is how can I iterate through the grid itself from one row to another row?
    for example, I select the 10th row in the grid and using a loop in the code, verify the value from this row to the next 20 rows.

    Thanks
    Last edited by Al2017; Jul 10th, 2018 at 11:18 AM.

  2. #2
    Frenzied Member gibra's Avatar
    Join Date
    Oct 2009
    Location
    ITALY
    Posts
    1,540

    Re: VB6 DataGrid, the grid row index vs the RecordSet row index

    Try to use Bookmark property, if the recordset support bookmarks

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2017
    Posts
    3

    Re: VB6 DataGrid, the grid row index vs the RecordSet row index

    Could you please explain your idea a bit more.
    I cannot imagine how.

  4. #4
    PowerPoster
    Join Date
    Jun 2013
    Posts
    3,659

    Re: VB6 DataGrid, the grid row index vs the RecordSet row index

    The thing with binding-scenarios is, that you should work more directly with the DataContainer-Object
    (the bound ADO-Rs in this case) instead of doing things with the bound Control (look at the Grid as only a "reflector of state").

    The following Code needs an empty Form + a reference to ADO and a DataGrid1 on that Form).
    Clicking the Form will ensure Sorting of the Grid-Content (by applying the Sort to the bound Rs) -
    followed by an enumeration of Rs-Content after the Sort was applied...

    Code:
    Option Explicit
    
    Private Rs As New Recordset
    
    Private Sub Form_Load()
      Rs.Fields.Append "ID", adInteger
      Rs.Fields.Append "Name", adVarWChar, 1024
      Rs.Open
      
      Rs.AddNew Array(0, 1), Array(3, "Item 3")
      Rs.AddNew Array(0, 1), Array(1, "Item 1")
      Rs.AddNew Array(0, 1), Array(5, "Item 5")
      Rs.AddNew Array(0, 1), Array(4, "Item 4")
      Rs.AddNew Array(0, 1), Array(2, "Item 2")
      Rs.MoveFirst
      
      Set DataGrid1.DataSource = Rs
      EnumerateRsAsShownInTheGrid Rs
    End Sub
    
    Private Sub Form_Click()
      Rs.Sort = "ID" 'this will automatically trigger a Grid-Refresh, reflecting the bound Rs' Sort-State
      EnumerateRsAsShownInTheGrid Rs
    End Sub
    
    Private Sub EnumerateRsAsShownInTheGrid(Rs As Recordset)
      With Rs.Clone 'Cloning ensures an independent Rs-Cursor (to not affect the current Record/Selections of the bound Grid-Rs)
        .Sort = Rs.Sort: .Filter = Rs.Filter 'reflect Sort and Filter-States also in the Clone
        
        Dim i As Long 'now enumerate (in current Sort-Order, if there was any set on the Rs)
        For i = 1 To .RecordCount
           .AbsolutePosition = i
          Debug.Print "Pos="; i, "ID="; !ID, "Name="; !Name
        Next
        
      End With
      Debug.Print
    End Sub
    HTH

    Olaf

  5. #5
    Frenzied Member gibra's Avatar
    Join Date
    Oct 2009
    Location
    ITALY
    Posts
    1,540

    Re: VB6 DataGrid, the grid row index vs the RecordSet row index

    Quote Originally Posted by Al2017 View Post
    Could you please explain your idea a bit more.
    I cannot imagine how.
    Make a project that reproduce the issue, zip-it and attach in the post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width