Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6240959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:40:05+00:00 2026-05-24T11:40:05+00:00

I have a TreeView in my Windows Form user interface. I want to fill

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:40:06+00:00Added an answer on May 24, 2026 at 11:40 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my C# Windows Form Application, I have Treeview control with checkboxes. I want
I have a TreeView windows forms control with an ImageList , and I want
I have a treeview control on an aspx page. The data comes from database
I have a TreeView in my form for which I dont want to have
I have a System.Windows.Controls.TreeView in my project, and when the user right click in
We have developed a User Interface as an MMC snap-in but we’re having issues
I have a control that inherits from TreeView (System.Windows.Controls.TreeView from WPF Framework) and it
I have my own custom control derived from System.Windows.Forms.TreeView which is present on the
I have a TreeView in my Windows application. Tn this TreeView, the user can
I have a treeview control in a Windows Forms project that has checkboxes turned

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.