This
step-by-step article describes how to populate a Microsoft ActiveX Data Objects
(ADO) Recordset and DataGrid without using a database connection. You may want
to use the DataGrid as a typical grid. When you do this, you can display
without binding the grid to any data source. This is useful when you use the
grid as a spreadsheet. To do this, you typically use other grids, such as the
Microsoft FlexGrid control. This article describes how to populate a DataGrid
without using a database connection as you do for FlexGrid.
Prerequisites
The following list outlines the recommended hardware, software,
network infrastructure, and service packs that are required for this procedure:
- Microsoft Visual Basic 6.0
This article assumes that you are familiar with the following
topics:
- Visual Basic 6.0 terminology and syntax
- Data Access technologies (ADO)
Populate the ADO Recordset
To populate the ADO Recordset, follow these steps:
- Open Visual Basic 6.0. On the File menu,
click New Project.
- In the New Project dialog box, click to
select Standard EXE, and then click
OK.
- On the Project menu, click
References.
- In the Available References list,
double-click to select Microsoft ActiveX Data Objects 2.5
Library, and then click OK.
- On the Project menu, click
Components.
- In the Component list, double-click to
select Microsoft DataGrid Control 6.0, and then click
OK.
- On the toolbox, double-click DataGrid
control.
DataGrid1 is created on the Form1. - Similarly, add two CommandButtons to Form1.
- Open the Code Editor, and then copy the following code:
Option Explicit
' Create a Recordset
Dim rst As ADODB.Recordset
Private Sub Command1_Click()
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseClient
' Add columns to the Recordset
rst.Fields.Append "Key", adInteger
rst.Fields.Append "Field1", adVarChar, 40, adFldIsNullable
rst.Fields.Append "Field2", adDate
' Open the Recordset
rst.Open , , adOpenStatic, adLockBatchOptimistic
' Add data to the Recordset
rst.AddNew Array("Key", "Field1", "Field2"), _
Array(1, "string1", Date)
rst.AddNew Array("Key", "Field1", "Field2"), _
Array(2, "string2", #1/1/2000#)
' Populate the Data in the DataGrid
Set DataGrid1.DataSource = rst
End Sub
Private Sub Command2_Click()
' Modify the data through code
rst.MoveFirst
rst(1) = "Changed Field"
rst.UpdateBatch
End Sub
Private Sub Form_Load()
Command1.Caption = "Populate"
Command2.Caption = "Update"
End Sub
Verify the Results
To verify the results, follow these steps:
- On the Run menu, click
Start to run the application.
- Click Populate to populate the DataGrid
with data.
- Modify the data "string2" in the second row to "Test
String".
- Click Update.
The data is
modified in both rows.
For
additional information, click the following article number to view the article
in the Microsoft Knowledge Base:
140021
(http://support.microsoft.com/kb/140021/EN-US/
)
FILE: DBGRIDUB.EXE Uses DBGRID in an Unbound Mode
Article ID: 313330 - Last Review: June 5, 2003 - Revision: 2.1
APPLIES TO
- Microsoft Visual Basic 6.0 Professional Edition
- Microsoft Visual Basic 6.0 Enterprise Edition
- Microsoft Visual Basic 6.0 Learning Edition
| kbdatabinding kbuidesign kbforms kbhowtomaster KB313330 |