Search code examples
telerikradtreeview

Allow only 8 selection in a Telerik radTreeListView


I have a radTreeListView with multiple parent nodes,which can contain multiple child nodes which can also contain multiple sub-child nodes.

I want to make sure only 8 items are selected and no more than that. Those 8 selections MUST also be under the same parent node.


Solution

  • You could try something like that:

    private RadTreeNode GetParentNode(RadTreeNode currentNode) {
       if (currentNode.Parent!=null) {
          return GetParentNode(currentNode.Parent);
       }   
       return currentNode;
    }
    
    private void CountSelectedNodes(RadTreeNode firstNode, ref int selectedCount) {
       if (firstNode.Nodes!=null) {
          foreach(RadTreeNode node in firstNode.Nodes) {
             CountSelectedNodes(node, ref selectedCount);
          }
          if (firstNode.IsSelected) {
              selectedCount+=1;
          }
       }
    }
    
    private bool SelectionAllowed(RadTreeNode currentNode) {
       RadTreeNode treeNode=GetParentNode(currentNode);
       int selectedCount;
       CountSelectedNodes(treeNode, ref selectedCount);
       return selectedCount<=8;
    }
    

    (this code is not tested so there could be some bugs)

    Call selectionAllowed() with the Node currently clicked