ADO Data Grid
Up

 

Using the Data Grid Control

To begin using the control, you have to add it to the toolbox. Make sure you don't select the data bound grid control.

Once you add the control to the form and run the application, you can see the unbound, useless grid.

Add an ADO Data control and set the connection to point to the Employees table in the Northwind database.

Set the data grid's data source to ADODC1 and run the application.

That's all there is to it. However, you may not want to generically display the recordset.

Right click on the data grid and choose properties

Property Description
AllowAddNew Can a new record be added?
AllowDelete Can a record be deleted?
AllowUpdate Can a record be updated?
ColumnHeaders Should column headers be displayed?
Enabled Do you want the user to be able to interact with the grid? If the grid is not large enough to display all the data, the user will not be able to scroll if the grid is not enabled.
Caption Text to appear directly above the grid
HeadLines The number of lines the column headers encompass
Appearance 3D or Flat
   

If you want to change the order of the properties as well as change the captions, use the 'Columns' tab. However, once you choose one column's order, you must choose ALL that you wish to add. You also have to add a caption or the column header will be blank. Hit Apply and move through successive fields.

You may run out of columns, so right click and choose 'edit.' Then right click again and choose 'append to add a new column to the end.

If you right click and choose 'retrieve fields', all of the original fields are added to the table. You can then change their order or captions accordingly. Right clicking and choosing 'clear fields' clears out all the fields and goes back to the default layout where you can't make any modifications to the layout.

The format tab allows you to choose a predefined format and apply it to a column.

You can also create your connection information in code:

Dim objConn As ADODB.Connection
Dim objRec As ADODB.Recordset

Private Sub Form_Load()
 On Error GoTo Connection_Error
     Set objConn = New ADODB.Connection
     objConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Program Files\Microsoft Visual Studio\VB98\NWIND.MDB;Persist Security Info=False"
     'This will only work with a client side cursor!!!
     objConn.CursorLocation = adUseClient
     objConn.Open
     Set objRec = New ADODB.Recordset
     
     objRec.Open "Employees", objConn, adOpenKeyset, adLockOptimistic, adCmdTable
     Set DataGrid1.DataSource = objRec
     Exit Sub
Connection_Error:
     MsgBox "Unable to Connect:" & vbCrLf & Err.Description, , "Connect"
End Sub

If you are adding a row to the datagrid, use the following code if you are adding to a row with an autonumber field.

Private Sub DataGrid1_AfterUpdate()
     objRec.Requery
End Sub