VB6.0 MSHFlexGrid控件添加数据三种方法 - 木子屋 和另外 4 个页面 - 用户配置 1

VB6.0 MSHFlexGrid控件添加数据三种方法编辑:dnawo 日期:2009-09-28字体大小: 小 中 大

1.绑定到数据源复制内容到剪贴板程序代码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 MSHFlexGrid1.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(MSHFlexGrid1.DataSource) <> "Nothing" ThenSet MSHFlexGrid1.DataSource = NothingEnd IfIf TypeName(objRs) <> "Nothing" ThenobjRs.CloseSet objRs = NothingEnd IfIf TypeName(objConn) <> "Nothing" ThenobjConn.CloseSet objConn = NothingEnd IfEnd Sub2.使用AddItem方法添加数据复制内容到剪贴板程序代码MSHFlexGrid1.Cols = 5MSHFlexGrid1.AddItem vbTab & "1" & vbTab & "2" & vbTab & "3" & vbTab & "4"MSHFlexGrid1.AddItem vbTab & "5" & vbTab & "6" & vbTab & "7" & vbTab & "8"MSHFlexGrid1.AddItem vbTab & "9" & vbTab & "10" & vbTab & "11" & vbTab & "12"MSHFlexGrid1.AddItem vbTab & "13" & vbTab & "14" & vbTab & "15" & vbTab & "16"'删除默认空白行'此操作不能放在 MSHFlexGrid1.Cols = 5 后,否则出错: Cannot remove last non-fixed row.MSHFlexGrid1.RemoveItem (1)3.逐一单元格添加数据复制内容到剪贴板程序代码Dim i, j, k As Integer'设置行列数With MSHFlexGrid1.Rows = 5.Cols = 5End With'添加表头MSHFlexGrid1.Row = 0For i = 1 To MSHFlexGrid1.Cols - 1MSHFlexGrid1.Col = iMSHFlexGrid1.Text = "列" & iNext'添加记录k = 1For i = 1 To MSHFlexGrid1.Rows - 1For j = 1 To MSHFlexGrid1.Cols - 1MSHFlexGrid1.Row = iMSHFlexGrid1.Col = jMSHFlexGrid1.Text = kk = k + 1NextNext说明:添加部件"Microsoft Hierarchical FlexGrid Control 6.0 (OLEDB)"才能使用MSHFlexGrid。

(0)

相关推荐