Link to home
Start Free TrialLog in
Avatar of netkuk
netkuk

asked on

Can I fill a DBgrid control without a Data control ?

I want to know if I can fill a DBgrid control without a Data control and which one is better to use (which is faster). In case that yes, say me the first steps that I have do. Thanks
Avatar of CarlosJac
CarlosJac

Please, excuse me, my english is terrific ...

Yes, you can fill a DBGrid Control Without a Data Control.
Set DataMode property to 1 (Unbound).
Fill an array with the info to show in the grid.

You must to code the UnboundReadData event.

This example illustrates the use of the UnboundReadData event to read a row of data from a simple array (named UserData).
You must fill the UserData array with the info to show in the grid

Private Sub DBGrid1_UnboundReadData(ByVal RowBuf As RowBuffer, StartLocation As Variant, ByVal ReadPriorRows As Boolean)
Dim CurRow&, Row%, Col%, RowsFetched%, Incr%
                   
If ReadPriorRows Then
    Incr% = -1
Else
    Incr% = 1
End If

' If StartLocation is Null then start reading at the
' end or beginning of the data set.
If IsNull(StartLocation) Then
    If ReadPriorRows Then
        CurRow& = RowBuf.RowCount - 1
    Else
        CurRow& = 0

    End If
Else
    ' Find the position to start reading based on the
    ' StartLocation bookmark and the lngIncr% variable
    CurRow& = CLng(StartLocation) + Incr%
End If

' Transfer data from our data set array to the RowBuf
' object which DBGrid uses to display the data
For Row% = 0 To RowBuf.RowCount - 1
    If CurRow& < 0 Or CurRow& >= mTotalRows& Then Exit For
    For Col% = 0 To UBound(UserData, 1)
        RowBuf.Value(Row%, Col%) = UserData(Col%, CurRow&)

    Next Col%
    ' Set bookmark using CurRow& which is also our
    ' array index
    RowBuf.Bookmark(Row%) = CStr(CurRow&)
    CurRow& = CurRow& + Incr%
    RowsFetched% = RowsFetched% + 1
Next Row%
RowBuf.RowCount = RowsFetched%
End Sub


Good Luck
Avatar of netkuk

ASKER

Gracias. No es que no me satisfaga la solución. Todo lo contrario ! Lo único es que te agardeceria que me dijeses, des de la posición de programador en la que trabajas, que método és más eficiente (ràpido). Muchas gracias Carlos i espero tu respuesta (por cierto mi inglés también es terrífico así que mejor en español contigo que me parece que lo sabes muy bien.)
ASKER CERTIFIED SOLUTION
Avatar of CarlosJac
CarlosJac

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
Avatar of netkuk

ASKER

Gracias por tu respuesta. ¡ Me ha ido de perfecta !


Por cierto, contestando a tu pregunta: soy de Barcelona, así que también hablo catalán.

Avatar of netkuk

ASKER

Por cierto, el ejemplo que me has dado lo he intentado implementar, o sea, lo he copiado en su correspondiente procedimiento del dbgrid (en el unbounddataread) y he creado el array userdata y lo he llenado. Sin embargo, cuando llamo al procedimiento con "dbgrid1_unbounddataread Rowbuff,1,true", me dice error 91. Yo he definido Rowbuf as Rowbuffer y luego Set Rowbuf=Rowbuffer. La verdad es que me he estado mirando la ayuda y no me acabo de enterar de como puñetas ver el contenido en el array.
Si me pudieras ayudar a completar el ejemplo que me has dado para que no me diera ese error y funcionara te lo agradeceria porque en lenguajes visuales tengo poca experiencia y tiendo a pensar y estructurar las cosas en "Topdown" con lo cual todos estos errores se me hacen una montaña.
Gracias de nuevo CarlosJac

Hola Netkuk:

Tu no tienes que ejecutar el evento unobundreaddata.  Tu tienes que llenar el vector userdata en el evento form_load.
Y tienes que copiar el codigo de ejemplo en el evento unboundreaddata de la grilla.  
Cuando la grilla necesita mostrar datos, por si sola ejecuta el evento unboundreaddata.
Tu no tienes que declarar Rowbuffer ni nada por el estilo.
Solo llenas el vector en el form_load y dejas que la grilla ejecute unboundreaddata cuando lo necesite.
Si me pasas tu direccion de correo electronico, puedo mandarte un form con una grilla unbound funcionando.

Suerte

CarlosJac
Avatar of netkuk

ASKER

Hola de nuevo CarlosJac.
Te digo los pasos que he hecho: he definido en "General" del formulario la tabla userdata(10,10) as string
He llenado mediante dos bucles anidados la tabla con mi nombre (p.ej)
He copiado el código que me dijiste y al ejecutar el pgm. no
me ha hecho nada.
Tambien he puesto un Dbgrid.refresh el el evento Click del Dbgrid
No sé si esto tiene algo que ver.
Te agradeceria que me pudieras enviar el código para que pudiera ver como tengo que ponerlo yo.
Puedes enviarme el códig a
guillrog_pratdesaba_prat@infomail.lacaixa.es
(un poquito larga...)
Gracias por las molestias CarlosJac.