开发者

Tree View not showing update

I have a TreeView in my Windows Form user interface. I want to fill it up from a database, but it does not refresh ever, even though if I WriteLine() every node, it is in memory as I expect.

In order to make it more easy to understand, I wrote a little example program that only has one button that creates a TreeView and a TreeView called treeView1 to display its content.

If anyone can tell me where I have misunderstood the use of the TreeView, it would be a tremendous help.

private void button1_Click(object sender, EventArgs e)
    {
        // create a tree
        TreeView t = new TreeView();
        TreeNode[] child = new TreeNode [1];
            child[0]=new TreeNode("myCat");
            child[0].Name = "IndependantOne";
            TreeNode categoryNode = new TreeNode("catIdTag", child);
            categoryNode.Name = "Citizen Cat 5239002147";
            t.Nodes.Add(categoryNode);
                // some stuff under the first node
                TreeNode[] mouseNode = new TreeNode[1];
                mouseNode[0] = new TreeNode("myMouse");
                mouseNode[0].Name = "SqueakyOne";
                TreeNode[] childItem = new TreeNode[1];
                childItem[0] = new TreeNode("mouseIdTag", mouseNode);
                childItem[0].Name = "Citizen Mouse 54655654649";
                TreeNode eltNode = new TreeNode("Cheese", childItem);
                eltNode.Name = "Emmental";
                t.Nodes["Citizen Cat 5239002147"].Nodes.Add(eltNode);

        // fill in the winform treeview
                if (t != null)
                {
                    //treeView1.Visible = false;
                    treeView1.BeginUpdate();
                    treeView1.Nodes.Clear();
                    treeView1.TopNode = t.TopNode;
                    foreach (TreeNode n in t.Nodes)
                    {
                        treeView1.Nodes.Add(n);
                        System.Diagnostics.Debug.WriteLine("Category Node contains:开发者_运维百科 " + treeView1.Nodes[n.Name].Name + " at " + treeView1.Nodes[n.Name].Level);
                        foreach (TreeNode no in treeView1.Nodes[n.Name].Nodes)
                        {
                            System.Diagnostics.Debug.WriteLine("Category Node Nodes contains: " + no.Name);
                        }
                    }
                    /*
                    This part I tried and it doesn't work either, still add it in the question if anyone knows if it's wiser?
                    this.treeView1 = t;
                    this.treeView1.Location = new System.Drawing.Point(233, 12);
                    this.treeView1.Name = "treeView1";
                    this.treeView1.Size = new System.Drawing.Size(351, 277);
                    this.treeView1.TabIndex = 11;
                    this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
                    */
                    treeView1.EndUpdate();
                    this.treeView1.Update();
                    this.treeView1.Refresh();

                    treeView1.Show();
                    //this.Refresh();
                }


    }

I also tried setting the treeView1 with

 treeView1 = t;

It was not a success...


change

treeView1.Nodes.Add(n);

with

treeView1.Nodes.Add((TreeNode)n.Clone());

You cannot host the same node in more than one TreeView control.

Alternatively, you can remove nodes from source TreeView before adding them to other TreeView:

while(t.Nodes.Count != 0)
{
    var node = t.Nodes[0];
    t.Nodes.RemoveAt(0);
    treeView1.Nodes.Add(node);
}

And I hope that there is a real reason why you create and fill tree view in your method instead of directly filling an already existing one. If it is not an intended behavior, remove the if block completely and change TreeView t = new TreeView(); to var t = treeView1.


This behavior is expected because with this code you`re confusing node-to-treeview relationship nature of the native .NET treeview control. Instead when moving nodes between treeviews (t -> treeView1) you need to clone them as suggested. Without that moved node still linked with the old treeview (see node.Treeview property) and because original tree (t) is not visible/added to any parent (form), I guess node will be invisible as well.

Also, the way you`re working with data loading (through creating new treeview) is pretty bad pattern. Instead you need to download your data (async I guess) into a temp buffer and recreate the treeView1 at once when data will be available with BeginUpdate/EndUpdate calls.

PS. Replacing treeView1 variable with 't' won't work as well because you don't replace the treeview control instance in the parent form/panel Controls property with this code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜