熱點推薦:
您现在的位置: 電腦知識網 >> 網絡技術 >> 正文

掌握和使用 TreeView 加載數據

2022-06-13   來源: 網絡技術 

  TreeView控件在編制程序過程中用得比較多正確的掌握和使用這個控件可以很好的表示層次關系的數據在數據庫應用系統中我們可以用TreeView來顯示樹形結構如顯示目錄樹顯示班級信息等等有著很重要的作用
  
  看看下面一個實際的問題每個年度都有許多的班級將這些數據層次表示
  
  數據庫:access
  
  表:Test
  
  字段:bh(班級編號文本主鍵)nj(年級文本)
  
  功能以層次關系表現出數據
  
  測試數據
  

  添加TreeView控件個按鈕
  
  個按鈕分別用了兩種方法顯示數據但是第二種方法明顯優於第一種
  
  可以仔細的看代碼
  
  引用microsoft activex data object x library
  
  Dim nddata As Node
  
  Dim cnn As ADODBConnection
  
  Dim rs As New ADODBRecordset
  
  Dim rs As New ADODBRecordset
  
  Private Sub Command_Click()
  
  On Error Resume Next
  
  Set nddata = TreeViewNodesAdd( db 班級信息)
  
  nddataExpanded = True
  
  Dim intcount As Integer
  
  Dim inttable As Integer
  
  Dim intfield As Integer
  
  Dim intfn As Integer
  
  rsOpen select nj from test group by nj cnn
  
  inttable = rsRecordCount
  
  Do While inttable <> intcount
  
  Set nddata = TreeViewNodesAdd(db tvwChild F & rsFields(nj) rsFields(nj))
  
  rsOpen select bhnj from test where nj= & rsFields(nj) & cnn
  
  intfield = rsRecordCount
  
  If intfield <> Then
  
  intfn =
  
  Do While intfield <> intfn
  
  Set nddata = TreeViewNodesAdd(F & rsFields(nj) tvwChild S & rsFields(bh) rsFields(bh))
  
  rsMoveNext
  
  intfn = intfn +
  
  Loop
  
  End If
  
  rsClose
  
  rsMoveNext
  
  intcount = intcount +
  
  Loop
  
  rsClose
  
  End Sub
  
  第二種方法
  
  Private Sub Command_Click()
  
  On Error Resume Next
  
  Set nddata = TreeViewNodesAdd( db 班級信息)
  
  nddataExpanded = True
  
  rsOpen select * from test cnn
  
  Do While Not rsEOF
  
  If ca <> rsFields(nj) Then
  
  Set nddata = TreeViewNodesAdd(db tvwChild F & rsFields(nj) rsFields(nj))
  
  ca = rsFields(bb)
  
  End If
  
  Set nddata = TreeViewNodesAdd(F & rsFields(nj) tvwChild S & rsFields(bh) rsFields(bh))
  
  rsMoveNext
  
  Loop
  
  rsClose
  
  End Sub
  
  Private Sub Form_Load()
  
  Set cnn = New ADODBConnection
  
  cnnOpen Provider=MicrosoftJetOLEDB;Data Source=F:\csdn_vb\database\treeview的節點添加\\testmdb
  
  End Sub
  
  Private Sub Form_Unload(Cancel As Integer)
  
  Set con = Nothing
  
  End Sub
  

From:http://tw.wingwit.com/Article/Network/201311/29939.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.