TreeList.VirtualTreeGetChildNodes Event

This event allows you to provide root and child nodes, when populating the Tree List control with data dynamically.

Namespace: DevExpress.XtraTreeList

Assembly: DevExpress.XtraTreeList.v20.1.dll

Declaration

[DXCategory("VirtualTree")]
public event VirtualTreeGetChildNodesEventHandler VirtualTreeGetChildNodes
<DXCategory("VirtualTree")>
Public Event VirtualTreeGetChildNodes As VirtualTreeGetChildNodesEventHandler

Event Data

The VirtualTreeGetChildNodes event's data class is VirtualTreeGetChildNodesInfo. The following properties provide information specific to this event:

Property Description
Children Gets or sets the collection of children for the currently processed business object.
Node Gets an instance of the business object being currently processed.

Remarks

The TreeList.VirtualTreeGetChildNodes, TreeList.VirtualTreeGetCellValue and TreeList.VirtualTreeSetCellValue events support dynamic data loading. Please refer to Virtual Mode (Dynamic Data Loading) Using Events (Tree List Level) to learn more.

A Tree List control supports dynamic data loading. To implement this data loading mode, set the TreeList.DataSource property to any object, except objects that implement the IList or IVirtualTreeListData interface. Then handle the VirtualTreeGetChildNodes and TreeList.VirtualTreeGetCellValue events, to provide data. To respond to an end-user changing node cells, handle the TreeList.VirtualTreeSetCellValue event.

The VirtualTreeGetChildNodes event fires on demand.

For more information and examples, see Virtual Mode (Dynamic Data Loading) Using Events (Tree List Level).

Examples

The example shows how to display information on directories and files on your system in a Tree List control dynamically, using only events. The full code for this example is available in the Explorer (Virtual Tree) module of the Tree List Main Demo. Here, only a part of the code is listed, giving you a basic understanding of how the Tree List events for dynamic loading work.

To provide data for the Tree List control, the TreeList.VirtualTreeGetChildNodes and TreeList.VirtualTreeGetCellValue events are handled. The first event is used to provide lists of root and child nodes. The second event is used to provide values for node cells. To save the changes made by an end-user to node cells, the TreeList.VirtualTreeSetCellValue event is handled. However, its implementation is omitted here. In this example, it's assumed that the Tree List control contains three columns, displaying a directory/file name, a directory flag and a file size. This data is provided via the VirtualTreeGetCellValue event.

To support dynamic data loading, the TreeList.DataSource property must be set to any custom object, except an IList object or an object implementing the IVirtualTreeListData interface. In the example, the DataSource property is set to a new Object instance.

The image below shows the result.

VirtualTreeEvents

// Specifies whether the root nodes that represent the local drivers are created.
bool loadDrives = false;
private void Form1_Load(object sender, System.EventArgs e) {
    treeList1.DataSource = new object();
}

// Indicates whether the processed node corresponds to the file.
bool IsFile(DirectoryInfo info){
    return (info.Attributes & FileAttributes.Directory) == 0;
}

// Initializes cell values.
private void treeList1_VirtualTreeGetCellValue(object sender, 
DevExpress.XtraTreeList.VirtualTreeGetCellValueInfo e) {
    DirectoryInfo di = new DirectoryInfo((string)e.Node);
    if (e.Column == treeListColumn1) 
            e.CellData = di.Name;
    if (e.Column == treeListColumn2) {
        if (!IsFile(di))
            e.CellData = "Folder";
        else 
            e.CellData = "File";
    }
    if (e.Column == treeListColumn3) {
        if (IsFile(di)){
            e.CellData = new FileInfo((string)e.Node).Length;
        }
        else e.CellData = null;
    }
}

// Creates and initializes child nodes.
private void treeList1_VirtualTreeGetChildNodes(object sender, 
DevExpress.XtraTreeList.VirtualTreeGetChildNodesInfo e) {
    if (!loadDrives){ // create drives
        string[] root = Directory.GetLogicalDrives();
        e.Children = root;
        loadDrives = true;
    }
    else {
        try{
            string path  = (string)e.Node;
            if(Directory.Exists(path)){
                string[] dirs = Directory.GetDirectories(path);
                string[] files = Directory.GetFiles(path);
                string[] arr = new string[dirs.Length + files.Length];
                dirs.CopyTo(arr,0);
                files.CopyTo(arr,dirs.Length);
                e.Children = arr;
            }
            else e.Children = new object[]{};
        }
        catch { e.Children = new object[]{}; }
    }
}

private void treeList1_VirtualTreeSetCellValue(object sender, VirtualTreeSetCellValueInfo e) {
    //...
}
See Also