1

Mark Rideout (former Microsoft programmer who developed the DataGridView control) posted a wonderful article that combined the DataGridView with the TreeView controls in 2006. It's a very useful control but has some very large drawbacks as far as speed and scale-ability. Several of which were addressed by users in the comments on the original blog post (https://blogs.msdn.microsoft.com/markrideout/2006/01/08/customizing-the-datagridview-to-support-expandingcollapsing-ala-treegridview/). But it's still VERY slow when loading thousands of rows with depth levels up to 10 deep.

I've read several articles that describe how to populate like 500K entries into a TreeView in less than a second using threads, but they're only 1 level deep, and the main issue I'm encountering is the code:

Classes.TreeGridNode Root = new Classes.TreeGridNode(); Root.Nodes.Add("test");

TreeGridNode does not support Cells without being attached to a TreeGridView. The Add() method tries to call node.Cells[0].Value = value; and gets a null exception because Cells is null. TreeView doesn't have that problem.

Is there a way to use threads to populate a large amount of TreeGridNodes on a TreeGridView? The original Mark Rideout blog has comments turned off and there doesn't appear to be support for that control anywhere else on the internet. Any pointers would be helpful, thank you!

I've tried creating a temporary TreeGridView to attach the nodes to, but I run into the threads cross-threading each other, which I can't totally wrap my brain around just yet.

    private void ButtonGenTree_Click(object sender, EventArgs eArgs)
    {
        _fillWorker.DoWork += new DoWorkEventHandler((o, e) =>
        {
            Stopwatch _stopWatch = new Stopwatch();
            //TreeNode Root = new TreeNode();
            // need to tie the treegridnode to a GRID otherwise the treegridnodecollection won't create a Cells() object for the node!
            Classes.TreeGridNode Root = new Classes.TreeGridNode();
            MessageBox.Show("root=" + Root.ToString());
            //Custom filling goes here instead of this simple for.
            for (int i = 0; i < 500000; i++)
            {
                MessageBox.Show("Loop i=" + i);
                Root.Nodes.Add(i.ToString());
            }
            _stopWatch.Stop();
            //Return our root node which contains all the TreeNodes and the elapsed time.
            e.Result = new object[2] { Root, _stopWatch.Elapsed.TotalMilliseconds };
        });

        _fillWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler((o, e) =>
        {
            Console.WriteLine("Operation took {0} milliseconds.", (e.Result as object[])[1]);
            this.treeView1.BeginUpdate();
            this.treeView1.Nodes.Add(((e.Result as object[])[0] as TreeNode));
            this.treeView1.EndUpdate();
        });

        _fillWorker.RunWorkerAsync();
    }

Would like to add nodes to a TreeGridNode independent of a TreeGridView so I can then append that Node tree to the view after the threading is done.

feelie75
  • 21
  • 4
  • Original TreeView threading code was found here that populated 500000 treeviews in < 1s: https://www.codeproject.com/Articles/27719/Populating-TreeView-on-a-Background-Thread – feelie75 Sep 23 '19 at 04:31
  • Do you need a recursive function? – Tim Rutter Sep 23 '19 at 04:46
  • Recursive or not, either way works. Right now I have a recursive solution, and it takes 10 seconds to Populate 15000 grid rows. I have an aircraft structure that takes 200K parts to construct, so taking 2 minutes to populate that kind of structure everytime a user views the tree won't work so I thought threads would help, just can't get the hang of it for this kind of gridview since a thread can't modify a grid that it didn't create (cross-threading exception is thrown). – feelie75 Sep 23 '19 at 04:51
  • reading the Related Threads links on the right-bar on this site led me to this post that looks very helpful and I will review tomorrow. it seems to have very useful diagrams to help conceptualize threading https://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the?rq=1 – feelie75 Sep 23 '19 at 05:17
  • So I tried TreeGridView, and ObjectListView (which is REALLY awesome, but super slow for trees with more than like 100,000 nodes). I finally landed on TreeView with Columns (https://www.codeproject.com/Articles/23746/TreeView-with-Columns and subsequently put into git by a commenter at https://gitlab.com/iegursoy28/treeviewdemo ). The latter TreeView with Columns can populate 80K nodes in 2s. I clicked up to a million and got results under 2s. And when you click Expand All, it expands all 1 million nodes in also less than 2 seconds. It's amazing. highly recommended. – feelie75 Sep 29 '19 at 05:08

0 Answers0