#region treeview联动方法 private void UpdateCheckStatus(TreeViewEventArgs e) { CheckAllChildNodes(e.Node); UpdateAllParentNodes(e.Node); } // updates all parent nodes recursively. private void UpdateAllParentNodes(TreeNode treeNode) { TreeNode parent = treeNode.Parent; if (parent != null) { if (parent.Checked && !treeNode.Checked) { parent.Checked = false; UpdateAllParentNodes(parent); } else if (!parent.Checked && treeNode.Checked) { bool all = true; foreach (TreeNode node in parent.Nodes) { if (!node.Checked) { all = false; break; } } if (all) { parent.Checked = true; UpdateAllParentNodes(parent); } } } } // updates all child tree nodes recursively. private void CheckAllChildNodes(TreeNode treeNode) { foreach (TreeNode node in treeNode.Nodes) { node.Checked = treeNode.Checked; if (node.Nodes.Count > 0) { // If the current node has child nodes, call the CheckAllChildsNodes method recursively. this.CheckAllChildNodes(node); } } } #endregion #region treeview上下级联动事件 private void tvDevc_AfterCheck(object sender, TreeViewEventArgs e) { if (e.Action != TreeViewAction.Unknown) { UpdateCheckStatus(e); } } #endregion