Skip to main content
All docs
V18.1

DevExpress v25.1 Update — Your Feedback Matters

Our What's New in v25.1 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

How to: Implement a Tree Structure for a Business Object

  • 4 minutes to read

The Virtual Mode by Binding to a Business Object topic covers information on binding a Tree List control to a business object. If the business object implements the IVirtualTreeListData, the Tree List control will dynamically create nodes based on the data stored in the object.

The following example shows how to implement a tree structure for a business object.

In the example, a custom MyData class will support the IVirtualTreeListData interface allowing objects of this class to be represented in a TreeList. The object implements all three methods of the interface.

  • VirtualTreeGetChildNodes - returns lists of root or child nodes.
  • VirtualTreeGetCellValue - returns values for node cells.
  • VirtualTreeSetCellValue - saves the changes made by an end-user to the object.

The MyData class is implemented as follows.


using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Columns;

// Represents a sample Business Object
public class MyData : TreeList.IVirtualTreeListData {
    protected MyData parentCore;
    protected ArrayList childrenCore = new ArrayList();
    protected object[] cellsCore;

    public MyData(MyData parent, object[] cells) {
        // Specifies the parent node for the new node.
        this.parentCore = parent;
        // Provides data for the node's cell.
        this.cellsCore = cells;
        if (this.parentCore != null) {
            this.parentCore.childrenCore.Add(this);
        }
    }
    void TreeList.IVirtualTreeListData.VirtualTreeGetChildNodes(
    VirtualTreeGetChildNodesInfo info) {
        info.Children = childrenCore;
    }
    void TreeList.IVirtualTreeListData.VirtualTreeGetCellValue(
    VirtualTreeGetCellValueInfo info) {
        info.CellData = cellsCore[info.Column.AbsoluteIndex];
    }

    void TreeList.IVirtualTreeListData.VirtualTreeSetCellValue(VirtualTreeSetCellValueInfo info) {
        cellsCore[info.Column.AbsoluteIndex] = info.NewCellData;
    }
}

The following code creates an object of the MyData class, representing a data source for the Tree List control, populates it with nodes and binds to the TreeList.


public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        InitData();
    }
    void InitData() {
        // Represents the root of the tree.
        MyData tlDataSource = new MyData(null, null);
        // Represents the first root node.
        MyData rootNode1 = new MyData(tlDataSource, new string[] { "Project A", "High" });
        MyData node1 = new MyData(rootNode1, new string[] { "Deliverable 1", "Normal" });
        MyData node2 = new MyData(node1, new string[] { "This task is mine A", "High" });
        MyData node3 = new MyData(node1, new string[] { "This task isn't mine A", "Low" });
        // Represents the second root node.
        MyData rootNode2 = new MyData(tlDataSource, new string[] { "Project B", "Normal" });
        // Represents the child node of the second root node.
        MyData node5 = new MyData(rootNode2, new string[] { "This task is mine B", "High" });
        TreeListColumn col1 = new TreeListColumn();
        col1.Caption = "Name";
        col1.VisibleIndex = 0;
        TreeListColumn col2 = new TreeListColumn();
        col2.Caption = "Priority";
        col2.VisibleIndex = 1;
        treeList1.Columns.AddRange(new TreeListColumn[] {col1, col2});            
        treeList1.DataSource = tlDataSource;
    }
}

The image below illustrates the result.

VirtualTreeInterface