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 8987199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:43:55+00:00 2026-06-15T21:43:55+00:00

In a C# Treeview, I need to remove all branches that do not contain

  • 0

In a C# Treeview, I need to remove all branches that do not contain values in a list in the leafs.
For example, I have a list { 112, 74 } and a Tree:
The Data Source is a self-joined table. So I can’t easily remove the unwanted leafs and branches.

Root
Branch1
Leaf 112
Leaf 1
Branch2
Leaf 74
Branch3
Leaf 44
Branch4
Leaf 99

I should end up with:
Root
Branch1
Leaf 112
Branch2
Leaf 74

Here is the code. It sort-of works. But the TrimTree only removes the bottom level. So I need to run this function a few times to completely remove all the empty branches. VERY inefficient.

    void GetTree(ref TreeView tv)
    {
     DataTable dt = c.GetTable("select id, parent_id, name from tbl_self_join_tree");
        tv.DataSource = new HierarchicalDataSet(dt, "ID", "Parent_ID");
        tv.DataBind();
    }

    void TrimTree(TreeNodeCollection nodes, List<string> l)
    {
        TreeNode node = null;
        for (int ndx = nodes.Count; ndx > 0; ndx--)
        {
            node = nodes[ndx - 1];
            if (node.ChildNodes.Count  == 0 && !l.Contains(node.Value))
                nodes.Remove(node);
            else   
                TrimTree(node.ChildNodes, l);
        }
    }

If I can write a better SELECT query, then I won’t need the TrimTree() function 😀
Solving this will help a LOT! Thank you so much!

  • 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-06-15T21:43:56+00:00Added an answer on June 15, 2026 at 9:43 pm

    It seems to me you can process parent nodes after processing child nodes. This way you can handle branches with all leafs deleted:

    //DISCLAIMER: I didn't compile or test this method.
    void TrimTree(TreeNodeCollection nodes, List<string> l)
    {
        TreeNode node = null;
        for (int ndx = nodes.Count; ndx > 0; ndx--)
        {
            node = nodes[ndx - 1];
            TrimTree(node.ChildNodes, l);
            if (node.ChildNodes.Count  == 0 && !l.Contains(node.Value))
                nodes.Remove(node);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a TreeView type controller that will display rich text. Example:
I have a TreeView control that I need to populate with a large 3
I have a asp.net treeview control that I need to be able to set
I have a TreeView that is bound to a tree of ViewModel instances. The
I need a control that behaves like a treeview (binds to a tree structure,
I need to have a TreeView with only vertical scrollbar. I tried this but
I have modified my treeview to look like an Org chart using the example
I have a TreeView control in a Windows Forms application that is displaying my
I need to create a TreeView in WPF that looks something like this: Admins
I have a treeview in which i need to set the font of only

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.