A common use of the DataGrid is to show "details" supplied by one table in a database. For example, the Northwind (Nwind.mdb) database includes two tables, one named "Suppliers," and the other named "Products." In this example, we'll use the DataList control to show the company names of suppliers from the "Suppliers" table. When the user clicks on any company name, the DataList control will furnish the SupplierID for the company. Using that ID, a query can be constructed to retrieve all records in the "Products" table which have a matching SupplierID. In other words, when the user clicks on a company (in the DataList control), all of the products produced by that company will appear in the DataGrid control.
To fill a DataGrid Control with products from a particular supplier
If the DataGrid and DataList and ADO Data controls are not present in the Toolbox, right-click the Toolbox, and click Components. In the Components dialog box double-click Microsoft DataGrid Control, Microsoft DataList Controls and Microsoft ADO Control.
Place the DataList control in the top left corner of the form, and place the DataGrid control somewhat below it.
Select the first ADO Data control and press F4 to display its Properties page. Set the Name property of the control to adoSuppliers. Select the second ADO Data control and set its Name property to adoProducts. Place the first control directly underneath the DataList Control, and the second directly below the DataGrid control.
Select the control named adoSuppliers and set the ConnectionString property to the Northwind OLE DB data source (Northwind.udl). Select the control named adoProducts and repeat the operation.
Select adoSuppliers and click RecordSource on the Properties page. Type Select * From Suppliers
. This query instructs the ADO Data control to return all records in the Suppliers table. Select adoProducts, click RecordSource, and type Select * From Products
. This query returns all the records from the Products table.
The RowSource property determines which data source supplies the data for the ListField property.
The ListField property is set to the name of a field in the table named Suppliers. At run-time, the DataList control displays the value of the field specified in this property. In this example, the property will display the name of a company found in the Suppliers table.
The BoundColumn property is set to a second field in the Suppliers table. In this case, the property is set to the SupplierID field. When the DataList control is clicked, the BoundText property returns the value of the SupplierID field associated with the company displayed in the DataList control. This value will be used in a query of the Products table, which provides data for the DataGrid control.
The DataSource property specifies the data source for the control. In this case, the property is set to the ADO Data control named adoProducts, which returns all of the records in the Products table.
Private Sub Datalist1_Click()
' Declare a string variable that will contain a new query. The
' new query uses the BoundText property of the DataList control
' to supply a SupplierID value. The new query simply asks for
' all products with the same SupplierID. This query is assigned
' to the RecordSource property of the ADO Data control named
' adoProducts. After refreshing the control, the DataGrid is
' updated with the new recordset of all products that are
' supplied by the same company.
Dim strQuery As String
strQuery = "Select * FROM Products WHERE SupplierID = " & _
Datalist1.BoundText
With adoProducts
.RecordSource = strQuery
.Refresh
End With
With DataGrid1
.ClearFields
.ReBind
End With
End Sub
Click any company name in the DataList control, and the DataGrid control is automatically updated with all products supplied by the company.