Need help with DBGrid in unbound mode

Need help with DBGrid in unbound mode

Post by Jim Lacso » Sat, 29 Nov 1997 04:00:00



I am trying to figure out how to populate a DBGrid control in unbound mode
with table data. I downloaded the DBGrid survival kit from Apex Software
which contains an example on how to do this (although the example loads an
array instead of table data) . I copied the code but replaced references to
the array with references to my recordset.

The problem is I am only getting the last set of  records from the recordset
to display in the grid. The problem seems to occur in the UnboundReadData
event of the DBGrid control. I set breakpoints in the event and confirmed
that the entire recordset is being loaded in to the RowBuf object. However,
this event fires several times (why?) before the grid is displayed and it
resets the RowBuf contents each time.

There is a loop that is conditioned in the rowcount property which seems to
be hardwired to a value of 10 which seems to cause the loop to terminate
before the entire recordset is loaded in to the grid. I hard coded the
number of records in the recordset (+/- 1) but  I get an "automation error"
which is probably caused by an EOF condition which I can't seem to trap
within the event.

I am new to VB & this is the first grid control I have tried. Is there
another grid control that is simpler to use?

TIA,
Jim

Here is the code:

Private Sub DBGrid1_UnboundReadData(ByVal RowBuf As MSDBGrid.RowBuffer,
StartLocation As Variant, ByVal ReadPriorRows As Boolean)
    ' DBGrid1 calls this routine each time it needs data.
    ' Keep track of how many rows have been provided by the
    ' programmer to the grid.
    Dim rowsFetched%
    rowsFetched% = 0

    ' See if the grid is requesting data forward or backward:
    '   If ReadPriorRows = True, then the grid is requesting
    '   rows prior to the StartLocation (backward).
    '   If ReadPriorRows = False, then the grid is requesting
    '   rows after the StartLocation (forward).
    Dim incr%
    If ReadPriorRows Then
        incr% = -1      ' Reading data backward
    Else
        incr% = 1       ' Reading data forward
    End If
    ' StartLocation is a bookmark supplied by the grid to
    ' indicate which records the grid wants to read.
    ' If StartLocation is NULL, then data is requested either
    ' from the first row or the last row.
    Dim curRow%
    If IsNull(StartLocation) Then
        If ReadPriorRows Then
           curRow% = MaxRow - 1  ' Reading data from the last row
        Else
            curRow% = 0           ' Reading data from the first row
        End If
    Else
        ' Reading data starting from the row next (previous) to
        ' StartLocation
        curRow% = Val(StartLocation) + incr%
    End If
    Dim i%
    i% = 0
    While i% < RowBuf.RowCount
      If curRow% < 0 Or curRow% >= MaxRow Then
            ' No more data to read, return RowCount and exit.  Note
            ' that the grid does not need to know the total number of
            ' rows ahead of time.  This is an important concept because
            ' this enables the unbound grid to operate under a virtual
            ' mode (i.e., it can be used to display unlimited rows of
            ' data) for any data you wish to display.
            RowBuf.RowCount = rowsFetched%
            Exit Sub
        End If
        ' Return the row data to the grid through the Row Buffer
        ' (RowBuf) object.

        RowBuf.Value(i%, 0) = RSTrndet("trndate")        ' automation error
appears on this line at EOF
        RowBuf.Value(i%, 1) = RSTrndet("trnno")

        RSTrndet.MoveNext

        ' Use the array row index as a bookmark and return it to
        ' the grid.
        RowBuf.Bookmark(i%) = curRow%
        curRow% = curRow% + incr%
        rowsFetched% = rowsFetched% + 1
        i% = i% + 1
    Wend
    ' Return the number of rows of data fetched
    RowBuf.RowCount = rowsFetched%
End Sub