HOW TO: Populate a DataGrid with an ADO Recordset without Using a Database Connection (313330)
The information in this article applies to:
- Microsoft Visual Basic Professional Edition for Windows 6.0
- Microsoft Visual Basic Enterprise Edition for Windows 6.0
- Microsoft Visual Basic Learning Edition for Windows 6.0
This article was previously published under Q313330 SUMMARYThis
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. Back to the topPrerequisitesThe 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)
Back to the topPopulate the ADO RecordsetTo 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
Back to the topVerify the ResultsTo 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. Back to the
topREFERENCES For
additional information, click the following article number to view the article
in the Microsoft Knowledge Base: 140021
FILE: DBGRIDUB.EXE Uses DBGRID in an Unbound Mode
Back to the
top
Modification Type: | Major | Last Reviewed: | 6/5/2003 |
---|
Keywords: | kbDataBinding KbUIDesign kbForms kbHOWTOmaster KB313330 kbAudDeveloper |
---|
|