This documentation is archived and is not being maintained.

TreeNodeCollection Class

Note: This class is new in the .NET Framework version 2.0.

Represents a collection of TreeNode objects in the TreeView control. This class cannot be inherited.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public ref class TreeNodeCollection sealed : ICollection, IEnumerable, IStateManager
public final class TreeNodeCollection implements ICollection, IEnumerable, 
	IStateManager
public final class TreeNodeCollection implements ICollection, IEnumerable, 
	IStateManager

The TreeNodeCollection class is used to store and manage a collection of TreeNode objects in the TreeView control. The TreeView control uses the TreeNodeCollection class in two of its properties. It stores its root nodes in the Nodes property and its selected nodes in the CheckedNodes property. The TreeNodeCollection collection is also used for the ChildNodes property to store child nodes (if any).

The TreeNodeCollection class supports several ways to access the items in the collection:

  • Use the Item indexer to retrieve a TreeNode object directly at a specific zero-based index.

  • Use the GetEnumerator method to create an enumerator that can be used to iterate through the collection.

  • Use foreach (C#) or For Each (Visual Basic) to iterate through the collection.

  • Use the CopyTo method to copy the contents of the collection into an Array object.

You can programmatically manage the TreeNodeCollection by adding and removing TreeNode objects. To add nodes to the collection, use the Add or AddAt method. To remove nodes from the collection, use the Remove, RemoveAt, or Clear method.

NoteNote

When the TreeView control is bound to a data source, the Nodes and ChildNodes collections are automatically populated each time binding occurs. Any changes to the collections between bindings will be lost. To retain these changes, either update the data source or manually rebuild the collection each time you bind.

The TreeNodeCollection contains properties and methods that allow you to retrieve information about the collection itself. To find out how many items are in the collection, use the Count property. If you want to determine whether the collection contains a certain TreeNode object, use the Contains method. To get the index of a TreeNode object in the collection, use the IndexOf method.

The following example demonstrates how to programmatically add nodes to and remove them from a TreeNodeCollection. Notice that the Nodes and ChildNodes properties return a TreeNodeCollection object.

No code example is currently available or this language may not be supported.

System.Object
  System.Web.UI.WebControls.TreeNodeCollection

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Show: