Search code examples
c#asp.netdevexpressdevexpress-windows-ui

DevExpress - Select multiple checkboxes in TreeList


Let's say if I have a TreeList of 2 groups, teachers and students, there is a column of "Status". I'm trying to implement a button to select all groups of teachers and students who are active.

enter image description here

This is what I have so far

public class MatchStatusOps : TreeListOperation
{
    private string fieldName;
    private string status;
    private TreeList helper;

    public TreeListMatchStatusOperation(string fieldName, string status,TreeList helper)
    {
        this.fieldName = fieldName;
        this.status = status;
        this.helper = helper;
    }

    public override void Execute(TreeListNode node)
    {
        String statusValue = Convert.ToString(node[fieldName]);
        if (statusValue.Equals(status))
            helper.SetNodeCheckState(node, CheckState.Checked, true);
    }
}

Then, I called it from my TreeList class

MatchStatusOps operation = new MatchStatusOps("Status","Active",this);
this.NodesIterator.DoOperation(operation);

I cannot make the check boxes selected, I think it may be because the node selected is the status nodes, not the checkbox nodes? Any ideas I could make it work? Thanks.


Solution

  • I think it may be because the node selected is the status nodes, not the checkbox nodes

    Those are not nodes, those are columns (usually bound by field name). A node can contain any number of columns.

    Further, this line of code ensures your operation is running across all nodes:

    NodesIterator.DoOperation(operation);
    

    The code below is based on an educated guess about your data source or code that populates the TreeList (if unbound) from the screenshot.

    Code:

    public class MatchStatusOps : TreeListOperation
    {
        private readonly string fieldName;
        private readonly string status;
        private readonly string checkboxFieldName;
    
        public MatchStatusOps(string fieldName, string status, string checkboxFieldName)
        {
            this.fieldName = fieldName;
            this.status = status;
            this.checkboxFieldName = checkboxFieldName;
        }
    
        public override void Execute(TreeListNode node)
        {
            String statusValue = Convert.ToString(node[fieldName]);
            if (statusValue.Equals(status))
                node[checkboxFieldName] = true;
        }
    }
    

    Usage:

    var operation = new MatchStatusOps("Status","Active","YourCheckboxFieldName");
    NodesIterator.DoOperation(operation);
    

    Note I'm setting a column on a given node to checked, not the node itself.

    Confirmed working on my end with latest DevExpress version as of this writing.