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.