/// <summary> /// Sort the contents of a TreeView alphabetically. /// </summary> /// <param name="treTree">TreeView to sort.</param> private void SortTree(TreeView treTree) { List <TreeNode> lstNodes = new List <TreeNode>(); foreach (TreeNode objNode in treTree.Nodes) { lstNodes.Add(objNode); } treTree.Nodes.Clear(); try { SortByName objSort = new SortByName(); lstNodes.Sort(objSort.Compare); } catch { } foreach (TreeNode objNode in lstNodes) { treTree.Nodes.Add(objNode); } }
/// <summary> /// Sort the contents of a TreeView alphabetically within each group Node. /// </summary> /// <param name="treView">TreeView to sort.</param> public static void SortCustom(this TreeView treView) { TreeNodeCollection lstTreeViewNodes = treView?.Nodes; if (lstTreeViewNodes == null) { return; } SortByName objSort = new SortByName(); for (int i = 0; i < lstTreeViewNodes.Count; ++i) { TreeNode objLoopNode = lstTreeViewNodes[i]; TreeNodeCollection objLoopNodeChildren = objLoopNode.Nodes; TreeNode[] lstNodes = new TreeNode[objLoopNodeChildren.Count]; objLoopNodeChildren.CopyTo(lstNodes, 0); objLoopNodeChildren.Clear(); Array.Sort(lstNodes, objSort.Compare); objLoopNodeChildren.AddRange(lstNodes); objLoopNode.Expand(); } }
/// <summary> /// Sort the contents of a TreeView alphabetically within each group Node. /// </summary> /// <param name="treTree">TreeView to sort.</param> public void SortTree(TreeView treTree) { for (int i = 0; i <= treTree.Nodes.Count - 1; i++) { List<TreeNode> lstNodes = new List<TreeNode>(); foreach (TreeNode objNode in treTree.Nodes[i].Nodes) lstNodes.Add(objNode); treTree.Nodes[i].Nodes.Clear(); try { SortByName objSort = new SortByName(); lstNodes.Sort(objSort.Compare); } catch { } foreach (TreeNode objNode in lstNodes) treTree.Nodes[i].Nodes.Add(objNode); treTree.Nodes[i].Expand(); } }
/// <summary> /// Sort the contents of a TreeView alphabetically. /// </summary> /// <param name="treTree">TreeView to sort.</param> private void SortTree(TreeView treTree) { List<TreeNode> lstNodes = new List<TreeNode>(); foreach (TreeNode objNode in treTree.Nodes) lstNodes.Add(objNode); treTree.Nodes.Clear(); try { SortByName objSort = new SortByName(); lstNodes.Sort(objSort.Compare); } catch { } foreach (TreeNode objNode in lstNodes) treTree.Nodes.Add(objNode); }