下列代码展示了如何在 非绑定模式 中使用节点来人工装载 TreeList 控件。

为了在非绑定模式中添加节点,使用了 TreeList.AppendNode 方法。 传递到此方法的数据应该与 TreeList 列匹配。 因此,在创建节点之前,创建了 TreeList 列。

注意,用于创建节点的方法调用被封闭在 TreeList.BeginUnboundLoadTreeList.EndUnboundLoad 方法之间。 这样可以把控件的更新降低到最少,从而提升性能。

下面的插图展示了代码运行结果:

C#CopyCode image复制代码
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;

private void Form1_Load(object sender, EventArgs e) {
    CreateColumns(treeList1);
    CreateNodes(treeList1);
}

private void CreateColumns(TreeList tl) {
    // Create three columns.
    tl.BeginUpdate();
    tl.Columns.Add();
    tl.Columns[0].Caption = "Customer";
    tl.Columns[0].VisibleIndex = 0;
    tl.Columns.Add();
    tl.Columns[1].Caption = "Location";
    tl.Columns[1].VisibleIndex = 1;
    tl.Columns.Add();
    tl.Columns[2].Caption = "Phone";
    tl.Columns[2].VisibleIndex = 2;
    tl.EndUpdate();
}

private void CreateNodes(TreeList tl) {
    tl.BeginUnboundLoad();
    // Create a root node .
    TreeListNode parentForRootNodes = null;
    TreeListNode rootNode = tl.AppendNode(
        new object[] { "Alfreds Futterkiste", "Germany, Obere Str. 57", "030-0074321" }, 
        parentForRootNodes);
    // Create a child of the rootNode
    tl.AppendNode(new object[] { "Suyama, Michael", "Obere Str. 55", "030-0074263" }, rootNode);
    // Creating more nodes
    // ...
    tl.EndUnboundLoad();
}
Visual BasicCopyCode image复制代码
Imports DevExpress.XtraTreeList
Imports DevExpress.XtraTreeList.Nodes

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    CreateColumns(treeList1)
    CreateNodes(treeList1)
End Sub

Private Sub CreateColumns(ByVal tl As TreeList)
    ' Create three columns.
    tl.BeginUpdate()
    tl.Columns.Add()
    tl.Columns(0).Caption = "Customer"
    tl.Columns(0).VisibleIndex = 0
    tl.Columns.Add()
    tl.Columns(1).Caption = "Location"
    tl.Columns(1).VisibleIndex = 1
    tl.Columns.Add()
    tl.Columns(2).Caption = "Phone"
    tl.Columns(2).VisibleIndex = 2
    tl.EndUpdate()
End Sub

Private Sub CreateNodes(ByVal tl As TreeList)
    tl.BeginUnboundLoad()
    ' Create a root node .
    Dim parentForRootNodes As TreeListNode = Nothing
    Dim rootNode As TreeListNode = tl.AppendNode(New Object() { "Alfreds Futterkiste", "Germany, Obere Str. 57", "030-0074321" }, parentForRootNodes)
    ' Create a child node for the node1            
    tl.AppendNode(New Object() { "Suyama, Michael", "Obere Str. 55", "030-0074263" }, rootNode)
    ' Creating more nodes
    ' ...
    tl.EndUnboundLoad()
End Sub