Skip to main content

How to: Load Data Dynamically via Events

  • 2 minutes to read

The following demo shows how to use the TreeList.VirtualTreeGetChildNodes and TreeList.VirtualTreeGetCellValue events to specify data for a Tree List control.

Run Demo: Explorer(Virtual Tree)

The navigationTreeList control in this demo displays directories of the file system. Instead of loading the entire directory structure on application startup, this control loads directories on demand (when a user expands a specific directory).

Tree List - Virtual mode events

The TreeList.VirtualTreeGetChildNodes event is handled to dynamically load child items (directories) for a specific node. The Tree List control automatically creates nodes for all child items that the VirtualTreeGetChildNodes event handler supplies.

The TreeList.VirtualTreeGetCellValue event handler specifies values for loaded children.

navigationTreeList.VirtualTreeGetChildNodes += OnNavigationTreeListGetChildNodes;
navigationTreeList.VirtualTreeGetCellValue += OnNavigationTreeListGetCellValue;
//...
void OnNavigationTreeListGetChildNodes(object sender, VirtualTreeGetChildNodesInfo e) {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    e.Children = ((Item)e.Node).GetDirectories();
    Cursor.Current = current;
}

void OnNavigationTreeListGetCellValue(object sender, VirtualTreeGetCellValueInfo e) {
    e.CellData = ((Item)e.Node).DisplayName;
}

//...
public abstract class Item : IFileImage {
    //...
    public string DisplayName {get; private set;}
    public abstract List<Item> GetDirectories();
}

Handle the TreeList.VirtualTreeSetCellValue event for the opposite task: if the Tree List is editable, you can write new cell values entered by users to a data source.

using DevExpress.XtraTreeList;

void OnVirtualTreeSetCellValue(object sender, VirtualTreeSetCellValueInfo e) {
    ((Item)e.Node).DisplayName = e.NewCellData.ToString();
}

See the Explorer (Virtual Tree) demo for the complete code.