tree view 後台編寫

本文介绍了一种使用 SqlAdapter 和 DataSet 在后台加载数据到 TreeView 控件的方法,并实现了节点选择变化时更新界面文本框内容的功能。此外,还提供了在 TreeView 控件中实现节点勾选状态同步更新其子节点及父节点状态的代码示例。
因為我要與數據庫聯系起來,所以必須後台編寫:
None.gif ' tree 綁定
ExpandedBlockStart.gifContractedBlock.gif
     Private   Sub Loadtree() Sub Loadtree()
InBlock.gif        
Dim myda As SqlDataAdapter
InBlock.gif        
Dim ds As DataSet = New DataSet
InBlock.gif        
Dim i As Integer = 0
InBlock.gif        
Dim ii As Integer = 0
InBlock.gif
InBlock.gif        
'系統資料類別
InBlock.gif
        mysql = "SELECT DISTINCT BasicData_Type FROM dbo.BasicData "
InBlock.gif
InBlock.gif        sqlcomm(mysql.Trim)
InBlock.gif        ds.Tables().Clear()
InBlock.gif        conn.sqlconn_Open()
InBlock.gif        myda 
= New SqlDataAdapter(mycomm)
InBlock.gif        myda.Fill(ds, 
"sys_type")
InBlock.gif        conn.sqlconn_Close()
InBlock.gif
InBlock.gif        mysql 
= "SELECT DISTINCT BasicData_Type, BasicData_Name, BasicData_Code"
InBlock.gif        mysql 
&= " FROM dbo.BasicData ORDER BY BasicData_Type"
InBlock.gif        sqlcomm(mysql.Trim)
InBlock.gif
InBlock.gif        conn.sqlconn_Open()
InBlock.gif        myda 
= New SqlDataAdapter(mycomm)
InBlock.gif        myda.Fill(ds, 
"sys_info")
InBlock.gif        conn.sqlconn_Close()
InBlock.gif
InBlock.gif        
'加入type
InBlock.gif
        Me.TreeView1.Nodes.Clear()
InBlock.gif
InBlock.gif        
For i = 0 To ds.Tables("sys_type").Rows.Count - 1
InBlock.gif            
Dim trestr As String = ds.Tables("sys_type").Rows(i)(0)
InBlock.gif            
Dim typeNode As New TreeNode(trestr.Trim)
InBlock.gif            TreeView1.Nodes.Add(typeNode)
InBlock.gif            
'加入對應info
InBlock.gif
            For ii = 0 To ds.Tables("sys_info").Rows.Count - 1
InBlock.gif                
Dim type As String = ds.Tables("sys_info").Rows(ii)(0)
InBlock.gif                
If trestr.Trim = type.Trim Then
InBlock.gif                    
Dim node As New TreeNode()
InBlock.gif                    node.Text 
= ds.Tables("sys_info").Rows(ii)(1)
InBlock.gif                    node.Value 
= ds.Tables("sys_info").Rows(ii)(2)
InBlock.gif                    typeNode.ChildNodes.Add(node)
InBlock.gif                
End If
InBlock.gif            
Next
InBlock.gif        
Next
InBlock.gif
ExpandedBlockEnd.gif    
End Sub

None.gif   ' 點擊tree編輯
ExpandedBlockStart.gifContractedBlock.gif
     Protected   Sub TreeView1_SelectedNodeChanged1() Sub TreeView1_SelectedNodeChanged1(ByVal sender As ObjectByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
InBlock.gif        
Me.TextBox3.Text = TreeView1.SelectedNode.Text
InBlock.gif        
Me.Hidden1.Value = TreeView1.SelectedNode.Value
InBlock.gif        
'sys_info()
ExpandedBlockEnd.gif
    End Sub

.Parent获得父结点,爷爷就是  
  ((TreeNode)((TreeNode)this.TreeView1.GetNodeFromIndex(this.TreeView1.SelectedNodeIndex).Parent).Parent).NodeData

判斷點擊的節點層:
Me.TreeView1.SelectedNode.Depth


private   void   NodeLostManagement(   string   errorID   )  
  {  
  //   Show   Message  
  this.ShowMessage(this.GetMessage(errorID));  
   
  //   Get   New   Tree   Info  
  DataTable   dtTree   =   new   DmsDB.TreeOperationDB().GetTreeData();  
   
  //   Old   Tree   SelectedIndex  
  string   oldTreePath   =   this.tvDocDir.SelectedNodeIndex;  
   
  //   New   Tree   SelectedIndex  
  string   newTreePath   =   string.Empty;  
   
  //   Check   Old   Node   (first   Check   Root   Node)  
  TreeNode   currentNode   =   this.tvDocDir.Nodes[0];  
   
  //   Split   OldSelectedIndex  
  string   []   oldNodeIndex   =   oldTreePath.Split('.');  
   
  for(int   i   =   0;i   <oldNodeIndex.Length   ;i++)  
  {  
  //   if   node   id   exists   in   the   new   tree    
  //   we   add   it   into   new   tree   selectedIndex  
  if(dtTree.Select("NodeId   ="   +   currentNode.ID).Length   >   0)  
  {  
  if(   i   ==   0   )  
  {  
  //   Move   Check   Node   into   next   node   (   Old   Tree   )  
  currentNode   =   currentNode.Nodes[int.Parse(oldNodeIndex[i+1])];  
  //   Root   Node    
  newTreePath   =   oldNodeIndex[i];  
  }  
  else  
  {  
  //   Move   Check   Node   into   next   node   (   Old   Tree   )  
  currentNode   =   currentNode.Nodes[int.Parse(oldNodeIndex[i+1])];  
  //   Sub   Node  
  newTreePath   =   newTreePath   +   "."   +   oldNodeIndex[i];  
  }  
  }  
  }  
   
  //   Rebuild   Tree  
  this.rebuildTree(newTreePath);  
   
  //   Refresh   the   screen  
  tvDocDir_SelectedIndexChange(   new   object()   ,   new   Microsoft.Web.UI.WebControls.TreeViewSelectEventArgs("0","0"));  
  }

Private   Sub   TreeView1_AfterCheck(ByVal   sender   As   Object,   ByVal   e   As   System.Windows.Forms.TreeViewEventArgs)   Handles   TreeView1.AfterCheck  
                  Try  
                          TreeView1.BeginUpdate()  
                          If   e.Action   <>   Windows.Forms.TreeViewAction.Unknown   Then  
                                  If   e.Node.Nodes.Count   >   0   Then  
                                          '   Calls   the   CheckAllChildNodes   method,   passing   in   the   current    
                                          '   Checked   value   of   the   TreeNode   whose   checked   state   changed.    
                                          Me.CheckAllChildNodes(e.Node,   e.Node.Checked)  
                                  End   If  
                                  If   Not   e.Node.Parent   Is   Nothing   Then  
                                          Me.CheckParentNodes(e.Node.Parent)  
                                  End   If  
                          End   If  
                          TreeView1.EndUpdate()  
                  Catch   ex   As   Exception  
                          Windows.Forms.MessageBox.Show(ex.Message)  
                  End   Try  
          End   Sub  
   
          Private   Sub   CheckAllChildNodes(ByVal   treeNode   As   Windows.Forms.TreeNode,   ByVal   nodeChecked   As   Boolean)  
                  Dim   node   As   Windows.Forms.TreeNode  
                  For   Each   node   In   treeNode.Nodes  
                          node.Checked   =   nodeChecked  
                          If   node.Nodes.Count   >   0   Then  
                                  '   If   the   current   node   has   child   nodes,   call   the   CheckAllChildsNodes   method   recursively.  
                                  Me.CheckAllChildNodes(node,   nodeChecked)  
                          End   If  
                  Next   node  
          End   Sub  
   
          Private   Sub   CheckParentNodes(ByVal   treeNode   As   Windows.Forms.TreeNode)  
                  Dim   node   As   Windows.Forms.TreeNode  
                  Dim   flag   As   Boolean   =   False  
                  For   Each   node   In   treeNode.Nodes  
                          If   node.Checked   Then  
                                  flag   =   True  
                          End   If  
                  Next  
                  treeNode.Checked   =   flag  
                  If   Not   treeNode.Parent   Is   Nothing   Then  
                          Me.CheckParentNodes(treeNode.Parent)  
                  End   If  
          End   Sub  
   
  你看一下这个代码。需要把treeview前面打checkbox打开。
http://topic.youkuaiyun.com/t/20010912/10/283301.html

转载于:https://www.cnblogs.com/Nina-piaoye/archive/2006/07/07/445085.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值