Populate XtraTreeList with dynamic nodes

1.1k views Asked by At

I have table with two columns:

+-------------+------------+  
| Level       | Desc       |  
+-------------+------------+  
| 1           | a          |  
+-------------+------------+    
| 2           | b          |  
+-------------+------------+ 
| 2           | c          | 
+-------------+------------+    
| 1           | d          |  
+-------------+------------+ 
| 2           | e          | 
+-------------+------------+    
| 2           | f          |  
+-------------+------------+ 
| 3           | g          | 
+-------------+------------+    
| 1           | h          |  
+-------------+------------+ 
| 1           | i          | 
+-------------+------------+ 
| 2           | j          |  
+-------------+------------+ 
| 2           | k          | 
+-------------+------------+ 

And I need to create display of this data in XtraTreeview with two columns according to Level column and it should be like:

- 1 a
   -- 2 b
   -- 2 c
 -1 d
   -- 2 e
   -- 2 f
      -- 3 g
 -1 h
 -1 i 
   -- 2 j 
   -- 2 k

So, level columns represents the node. Level 1 is the main node, level 2 is subnode of level 1, level 3 is subnode of level 2, level 4 is subnode of 3... I know how to populate Xtratreeview when there is fixed numbers of nodes and subnodes but in this case don't have idea how to populate where 1 node consist 3, 4 or more subnodes.

I've done this so far:

Populate TreeView:

DataTable table = new DataTable();
        table.Columns.Add("Level");
        table.Columns.Add("Data");

        table.Rows.Add(1, "a");
        table.Rows.Add(2, "b");
        table.Rows.Add(2, "c");
        table.Rows.Add(1, "d");
        table.Rows.Add(2, "e");
        table.Rows.Add(2, "f");
        table.Rows.Add(3, "g");
        table.Rows.Add(4, "z");
        table.Rows.Add(5, "x");
        table.Rows.Add(2, "h");
        table.Rows.Add(3, "i");
        table.Rows.Add(1, "j");
        table.Rows.Add(2, "k");

        TreeListNode rootNode = null;

        for (int i = 0; i < table.Rows.Count; i++)
        {
            tl.BeginUnboundLoad();

            TreeListNode parentForRootNodes = null;

            if (table.Rows[i][0].ToString().Equals("1"))
            {
                rootNode = tl.AppendNode(new object[] { (string)table.Rows[i][1] }, parentForRootNodes);
            }


            if (table.Rows[i][0].ToString().Equals("2"))
            {
                tl.AppendNode(new object[] { (string)table.Rows[i][1] }, rootNode);
            }

            tl.EndUnboundLoad();
        }

Create columns:

 private void CreateColumns2(TreeList tl)
    {
        tl.BeginUpdate();
        tl.Columns.Add();
        tl.Columns[0].Caption = "Level";
        tl.Columns[0].VisibleIndex = 0;
        tl.Columns.Add();
        tl.Columns[1].Caption = "Desc";
        tl.Columns[1].VisibleIndex = 1;
        tl.Columns.Add();
        tl.EndUpdate();
    }
1

There are 1 answers

0
atom.gregg On

Documentation you might like to read is here: https://documentation.devexpress.com/#windowsforms/CustomDocument198

You need at minimum three things for a tree:

  • Id
  • ParentId
  • Text

So the structure you've described needs to change to permit finding the parent for an item.

Once you have that, the concept goes like this:

  • Create an item for each node you want in the tree, I created my own class for this with the properties I wanted (Id, ParentId, Text...)
  • Then set the datasource of the tree control

Example:

var data = new List<TreeItem>
{
    new TreeItem { Id = "L1_1", ParentId = "", Text = "ONE" },
    new TreeItem { Id = "L1_2", ParentId = "", Text = "TWO" },
    new TreeItem { Id = "L1_3", ParentId = "", Text = "THREE" },
    new TreeItem { Id = "L2_1", ParentId = "L1_1", Text = "A" },
    new TreeItem { Id = "L2_2", ParentId = "L1_1", Text = "B" },
    new TreeItem { Id = "L2_3", ParentId = "L1_2", Text = "C" },
    new TreeItem { Id = "L2_4", ParentId = "L1_2", Text = "D" },
    new TreeItem { Id = "L2_5", ParentId = "L1_2", Text = "E" }
};

tree.Properties.DataSource = data;

}

class TreeItem
{
    public string Id { get; set; }
    public string ParentId { get; set; }
    public string Text { get; set; }
}

The order of the items in the data source is irrelevant, what is important is the uniqueness of each id.

The above example produces a tree like this:

- ONE
-- A
-- B
- TWO
- THREE
-- C
-- D
-- E

I am doing this without my DevExpress installation and without a compiler, so please excuse any errors. however the concept remains the same.