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 VirtualTreeGetChildNodes and 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 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 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.

C# |
bool loadDrives = false;
private void Form1_Load(object sender, System.EventArgs e) {
treeList1.DataSource = new object();
}
bool IsFile(DirectoryInfo info){
return (info.Attributes & FileAttributes.Directory) == 0;
}
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;
}
}
private void treeList1_VirtualTreeGetChildNodes(object sender,
DevExpress.XtraTreeList.VirtualTreeGetChildNodesInfo e) {
if (!loadDrives){
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) {
}
|
VB |
Dim loadDrives As Boolean = False
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
TreeList1.DataSource = New Object()
End Sub
Function IsFile(ByVal info As DirectoryInfo) As Boolean
Return (info.Attributes And FileAttributes.Directory) = 0
End Function
Private Sub TreeList1_VirtualTreeGetCellValue(ByVal sender As Object, _
ByVal e As DevExpress.XtraTreeList.VirtualTreeGetCellValueInfo) _
Handles TreeList1.VirtualTreeGetCellValue
Dim di As New DirectoryInfo(CStr(e.Node))
If e.Column Is TreeListColumn1 Then
e.CellData = di.Name
End If
If e.Column Is TreeListColumn2 Then
If Not IsFile(di) Then
e.CellData = "Folder"
Else
e.CellData = "File"
End If
End If
If e.Column Is TreeListColumn3 Then
If IsFile(di) Then
e.CellData = New FileInfo(CStr(e.Node)).Length
Else
e.CellData = Nothing
End If
End If
End Sub
Private Sub TreeList1_VirtualTreeGetChildNodes(ByVal sender As Object, _
ByVal e As DevExpress.XtraTreeList.VirtualTreeGetChildNodesInfo) _
Handles TreeList1.VirtualTreeGetChildNodes
If loadDrives = False Then
Dim root As String() = Directory.GetLogicalDrives()
e.Children = root
loadDrives = True
Else
Try
Dim path As String = CStr(e.Node)
If Directory.Exists(path) Then
Dim dirs As String() = Directory.GetDirectories(path)
Dim files As String() = Directory.GetFiles(path)
Dim arr(dirs.Length + files.Length) As String
dirs.CopyTo(arr, 0)
files.CopyTo(arr, dirs.Length)
e.Children = arr
Else
e.Children = New Object() {}
End If
Catch
End Try
End If
End Sub
Private Sub TreeList1_VirtualTreeSetCellValue(ByVal sender As System.Object, _
ByVal e As DevExpress.XtraTreeList.VirtualTreeSetCellValueInfo) _
Handles TreeList1.VirtualTreeSetCellValue
End Sub
|