Back to Devexpress

TreeList.Selection Property

windowsforms-devexpress-dot-xtratreelist-dot-treelist-8696558a.md

latest5.7 KB
Original Source

TreeList.Selection Property

Gets the collection of selected nodes.

Namespace : DevExpress.XtraTreeList

Assembly : DevExpress.XtraTreeList.v25.2.dll

NuGet Packages : DevExpress.Win.Navigation, DevExpress.Win.TreeList

Declaration

csharp
[Browsable(false)]
public TreeListSelection Selection { get; }
vb
<Browsable(False)>
Public ReadOnly Property Selection As TreeListSelection

Property Value

Type
DevExpress.XtraTreeList.TreeListSelection

Remarks

When a user navigates through the TreeList control, the TreeList.Selection collection contains a selected node. If the TreeListOptionsSelection.MultiSelect option is active and miltiple nodes are selected, the TreeList.Selection collection contains all selected nodes.

To enumerate elements of the collection, you can use the foreach ( For Each in Visual Basic) statement, as the following code snippet demonstrates.

csharp
string columnName = "DEPARTMENT";
int i = 0;
foreach (DevExpress.XtraTreeList.Nodes.TreeListNode treeListNode in treeList1.Selection) {
    //Set a new value in the Department column for each selected node.
    treeListNode.SetValue(columnName, "Department" + i++.ToString());
}
vb
Dim columnName As String = "DEPARTMENT"
Dim i As Integer = 0
For Each treeListNode As DevExpress.XtraTreeList.Nodes.TreeListNode In TreeList1.Selection
    'Set a new value in the Department column for each selected node.
    treeListNode.SetValue(columnName, "Department" + i.ToString())
    i += 1
Next

Example

The following code shows how to select children of a specific node. In the example, nodes are selected via the TreeListMultiSelection.Set method of the TreeList.Selection object.

The image below shows the result of selecting a Sales and Marketing node’s children.

csharp
using DevExpress.XtraTreeList.Nodes;

TreeListNode node = treeList1.FindNodeByFieldValue("Department", "Sales and Marketing");
if (node != null) {
    ArrayList selectedNodes = new ArrayList();
    selectChildren(node, selectedNodes);
    treeList1.Selection.Set(selectedNodes);
}

void selectChildren(TreeListNode parent, ArrayList selectedNodes) {
   IEnumerator en = parent.Nodes.GetEnumerator();
   TreeListNode child;
   while(en.MoveNext()) {
      child = (TreeListNode)en.Current;
      selectedNodes.Add(child);
      if(child.HasChildren) selectChildren(child, selectedNodes);
   }
}
vb
Imports DevExpress.XtraTreeList.Nodes

Dim node As TreeListNode = treeList1.FindNodeByFieldValue("Department", "Sales and Marketing")
If node IsNot Nothing Then
    Dim selectedNodes As New ArrayList()
    selectChildren(node, selectedNodes)
    treeList1.Selection.[Set](selectedNodes)
End If

Sub SelectChildren(ByVal parent As TreeListNode, ByVal selectedNodes As ArrayList)
   Dim en As IEnumerator = parent.Nodes.GetEnumerator()
   Dim child As TreeListNode
   While en.MoveNext() = True
      child = CType(en.Current, TreeListNode)
      selectedNodes.Add(child)
      If child.HasChildren = True Then
         selectChildren(child, selectedNodes)
      End If
   End While
End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the Selection property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-treelist-save-restore-expanded-state-of-nodes/CS/TreeListViewState.cs#L34

csharp
ArrayList al = new ArrayList();
foreach(TreeListNode node in TreeList.Selection)
    al.Add(node.GetValue(TreeList.KeyFieldName));

winforms-treelist-save-restore-expanded-state-of-nodes/VB/TreeListViewState.vb#L37

vb
Dim al As New ArrayList()
For Each node As TreeListNode In TreeList.Selection
    al.Add(node.GetValue(TreeList.KeyFieldName))

See Also

TreeList.CopyToClipboard

Node Selection

ClearSelection()

TreeList Class

TreeList Members

DevExpress.XtraTreeList Namespace