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

  • Home
  • SEARCH
  • 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 8568257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:04:25+00:00 2026-06-11T18:04:25+00:00

I am trying to make a treeView that have items in the following order.

  • 0

I am trying to make a treeView that have items in the following order.

+ Continent
    + Country
        + Province
            + Territory

And, I have a generic class to hold the treeView Items data.

public class TreeViewContinents
{
    public List<TreeViewContinents> Children { get; set; }    
    public TreeViewContinents Parent { get; set; }    
    public string Name { get; set; }    
    public string Content { get; set; }
}

Data:

private readonly List<Dictionary<string, object>> continentsList = new List<Dictionary<string, object>>();

/*
continentsList[0] - "Continent", "Asia"
                    "Country", "Afghanistan"
                    "Province", "Kabul"
                    "Territory", "Bagrami"
                    "Language", "aaa"
                    "Culture", "bbb"
                .
                .               
                .
                .

continentsList[n] - "Continent", "North America"
                    "Country", "Canada"
                    "Province", "Ontario"
                    "Territory", "Ottawa"
                    "Language", "aaa"
                    "Culture", "bbb

*/

String array based on which treeView Items needs to be generated.

var treeViewItemNames = new[] { "Continent", "Country", "Province", "Territory" };

Code:

List<object> Continents = this.continentsList.Where(
                            p_oDict => p_oDict.ContainsKey(treeViewItemNames[0]))
                            .Select( p_oDict => p_oDict[treeViewItemNames[0]])
                            .Distinct()
                            .ToList();

var topItems = new List<TreeViewContinents>();

foreach (object continent in Continents)
{
    var level1Items = new TreeViewContinents { Name = treeViewItemNames[0], Content = continent.ToString() };

    List<object> Countries = this.continentsList.Where(
                            p_oDict => p_oDict.ContainsKey(treeViewItemNames[0])
                            && p_oDict.ContainsKey(treeViewItemNames[1])
                            && p_oDict[treeViewItemNames[0]].Equals(continent)
                            .Select( p_oDict => p_oDict[treeViewItemNames[1]])
                            .Distinct()
                            .ToList();

    Countries.Sort((p_oU1, p_oU2) => string.Compare(p_oU1.ToString(), p_oU2.ToString(), StringComparison.Ordinal));
    foreach (object country in Countries)
    {
        var level2Items = new TreeViewContinents { Name = treeViewItemNames[1], Content = country.ToString(), Parent = level1Items };
        level1Items.Children.Add(level2Items);

        List<object> Provinces = this.continentsList.Where(
                               p_oDict => p_oDict.ContainsKey(treeViewItemNames[0])
                                        && p_oDict.ContainsKey(treeViewItemNames[1])
                                          && p_oDict.ContainsKey(treeViewItemNames[2])
                                          && p_oDict[treeViewItemNames[0]].Equals(continent)
                                          && p_oDict[treeViewItemNames[1]].Equals(country))
                                          .Select( p_oDict => p_oDict[treeViewItemNames[2]]).Distinct()
                                        .ToList();

        Provinces.Sort((p_oU1, p_oU2) => string.Compare(p_oU1.ToString(), p_oU2.ToString(), StringComparison.Ordinal));
        foreach (object province in Provinces)
        {
            var level3Items = new TreeViewContinents { Name = treeViewItemNames[2], Content = province.ToString(), Parent = level2Items };
            level2Items.Children.Add(level3Items);

            List<object> Territories = this.continentsList.Where(
                               p_oDict => p_oDict.ContainsKey(treeViewItemNames[0])
                                        && p_oDict.ContainsKey(treeViewItemNames[1])
                                        && p_oDict.ContainsKey(treeViewItemNames[2])
                                        && p_oDict.ContainsKey(treeViewItemNames[3])
                                        && p_oDict[treeViewItemNames[0]].Equals(continent)
                                        && p_oDict[treeViewItemNames[1]].Equals(country)
                                        && p_oDict[treeViewItemNames[2]].Equals(province)).Select(
                                        p_oDict => p_oDict[treeViewItemNames[3]])
                                        .Distinct()
                                        .ToList();
            Territories.Sort((p_oU1, p_oU2) => string.Compare(p_oU1.ToString(), p_oU2.ToString(), StringComparison.Ordinal));
            foreach (object territory in Territories)
            {
                var level4Items = new TreeViewContinents { Name = treeViewItemNames[3], Content = territory.ToString(), Parent = level3Items };
                level3Items.Children.Add(level4Items);
            }
        }
    }

    topItems.Add(level1Items);
}

In the above code, I have four foreach loops to build a treeview. Is it possible to write a single loop to generate the treeView based on the the variable treeViewItemNames, so that in future If i change the variable like

var treeViewItemNames = new[] { "Continent", "Country", "Territory" };

it should generate treeView like this

+ Continent
    + Country
        + Territory

Ideas/ Suggestions please.

  • 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-11T18:04:26+00:00Added an answer on June 11, 2026 at 6:04 pm

    I suggest to iterage through continentsList elements.

        var treeViewItemNames = new[] { "Continent", "Country", "Province", "Territory" };
    
         var topItems = new List<TreeViewContinents>();
    
    
         foreach (var continent in continentsList)
         {
             List<TreeViewContinents> currentLevel = topItems;
             TreeViewContinents parentItem = null;
             foreach (var sectionTitle in treeViewItemNames)
             {
                 String value = Convert.ToString(continent[sectionTitle]);
                 TreeViewContinents currentItem = currentLevel.FirstOrDefault(tree => tree.Content == value);
                 if (currentItem == null)
                 {
                     currentItem = new TreeViewContinents { Name = sectionTitle, Content = value };
                     currentItem.Children = new List<TreeViewContinents>();
                     if (parentItem != null)
                     {
                         currentItem.Parent = parentItem;
                     }
                     currentLevel.Add(currentItem);
                 }
                 parentItem = currentItem;
                 currentLevel = currentItem.Children;
             }
         }
    

    The problem with this code is that there is no sorting in items, but we can sort the resulting TreeViewContinents list. Here is recursive method for sorting:

    public List<TreeViewContinents> SortTreeView(List<TreeViewContinents> treeViewList)
    {
        foreach (var item in treeViewList)
        {
            if (item.Children.Count > 0)
            {
                item.Children = SortTreeView(item.Children);
            }
        }
        return treeViewList.OrderBy(it => it.Content).ToList();
    }
    

    You can use it after topItems list is full:

    topItems = SortTreeView(topItems);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a treeview with items that has both image and text.
I have been trying to make a treeview that looks something like 2001(root) -Student1(node)
I'm trying to validate items inside a treeview. The main idea is that a
I have some TreeView's in my winform application. I am trying to make mass
I am trying to make a WPF Application containing a treeview, whose data will
I'm trying make an entity with doctrine that has three associations with other entities
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
I'm trying to make a tabbed user interface using an asp:Panel and the asp:TreeView.
I'm trying to make a report treeview thing. Where I could write some text
I have TreeView Populating from Database And I am Trying to detect the user

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.