VB6.0编程给DataGrid绑定数据示例 - 木子屋 和另外 4 个页面 - 用户配置 1

VB6.0编程给DataGrid绑定数据示例编辑:dnawo 日期:2009-09-28字体大小: 小 中 大

①选择菜单"工程→部件",添加"Microsoft DataGrid Control 6.0 (OLEDB)",从工具箱中拖个DataGrid到窗体上;②选择菜单"工程→引用",添加"Microsoft ActiveX Data Objects 2.6 Library",这样在代码就可以使用ADO对象了;③添加以下代码:复制内容到剪贴板程序代码Option ExplicitDim objConn As ADODB.ConnectionDim objRs As ADODB.RecordsetPrivate Sub Form_Load()On Error Resume NextSet objConn = New ADODB.ConnectionSet objRs = New ADODB.RecordsetobjConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\demo3.mdb;"objConn.OpenobjRs.CursorLocation = adUseClientobjRs.Open "Select ProductId,ProductName,Discontinued FROM Products", objConn, adOpenStatic, adLockReadOnlySet DataGrid1.DataSource = objRsIf Err.Number <> 0 ThenMsgBox Err.DescriptionEnd IfOn Error GoTo 0End Sub'释放资源Private Sub Form_Unload(Cancel As Integer)'不操作此步会出错:The current row is not availableIf TypeName(DataGrid1.DataSource) <> "Nothing" ThenSet DataGrid1.DataSource = NothingEnd IfIf TypeName(objRs) <> "Nothing" ThenobjRs.CloseSet objRs = NothingEnd IfIf TypeName(objConn) <> "Nothing" ThenobjConn.CloseSet objConn = NothingEnd IfEnd Sub运行程序,可以了^_^

(0)

相关推荐