Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filling Data Grid from DB WITHOUT adodc 1

Status
Not open for further replies.

Overdoos

IS-IT--Management
May 31, 2000
79
BE
Hi,

I seem to be missing something and I can't seem to figure out what that is.

For some little VB project, I'm trying to configure a datagrid that will hold all the commandlines for a specific customer. The customers are chosen via a tree-structure (that works perfectly) and this tree sends the cust_ID out to whatever screen is selected. This goes well for all screens except for the datagrid one.

What I would like to know is:
Can I dynamicaly set up a datagrid?
If I can, how do I populate it via ADO-connection sending an SQL-string to the db?
How do I show the contained data?

Anybody got some ideas?
 
if you build a recordset from your sql string you can use that to populate your grid manually.


you may also be able to use a recordset as the datasource for the grid, but i'm not too sure on that. My preference would be to fill it manually anyway.


Matt

If you can keep your head while those around you are losing theirs, you obviously haven't grasped the seriousness of the situation
 
Thank you Matt,

I forgot to mention that I did already have a recordset, but that I couldn't figure out how to put the data in the columns. Heck, for some reason, I can't even seem to be able to create columns (although [datagrid].columns.count give's me 10 as a result :/ )

Have you got the time to elaborate on this please?
 
ok,

This was a quick test, as i don't normally use datagrids, I use flexgrids and fill them manually. It doesn't appear that you can do that with a datagrid.

They do accept ADO recordsets as the datasource though.

So for this example open a new project, add a datagrid(datagrid1) and a command button (command1)
and a reference to ADO. Then paste this into the form code

(Make sure your use a good connection string and sql query string!)

Code:
Option Explicit

Dim cn As ADODB.Connection, rs As ADODB.Recordset

Private Sub Command1_Click()
    Set rs = New ADODB.Recordset
    rs.ActiveConnection = cn
    rs.Source = "YOUR SQL QUERY"
    rs.CursorLocation = adUseClient
    rs.CursorType = adOpenDynamic
    rs.LockType = adLockOptimistic
    rs.Open
    Set DataGrid1.DataSource = rs
    DataGrid1.ReBind
End Sub

Private Sub Form_Load()
Set cn = New ADODB.Connection
    cn.ConnectionString = "YOURCONNECTIONSTRING"
    cn.Open
End Sub

Private Sub Form_Unload(Cancel As Integer)
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Sub

This will fill the grid with your data.

Hopefully this will help you out, at least by giving you a starting point.

Matt

If you can keep your head while those around you are losing theirs, you obviously haven't grasped the seriousness of the situation
 
Thanks.

This seems to do what I want it to. I'll implement this in the application.

I heard you talking about flexgrids... Do they offer more/better functionality than datagrids?

I'm just asking because I'm new to this whole VB-thing and I would like to get the best result possible for a VB-novice like myself ;)
 
There are two types of flexgrid. The basic flex grid and the hierarchical flexgrid.

The basic grid is just that a grid, you can set virtually anything you want in it. You add the data a row at a time and change change it on the fly by usign current row/col or using the cell method(very useful). You cant enter information in to it though, but it is not difficult to add this functionality in to it.

Check out the following links



It is probably a personal choice really. I like to use flexgrids, others may prefer the datagrid.

See which one you like, and more importantly, which one better fits your needs.

Matt

If you can keep your head while those around you are losing theirs, you obviously haven't grasped the seriousness of the situation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top