





















































.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