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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:19:44+00:00 2026-05-20T07:19:44+00:00

I have a datatable which i get from the databasemanager class, and i would

  • 0

I have a datatable which i get from the databasemanager class,
and i would need to transform that data into a hierarchical structure

either binding it directly to a .net treeview control or in memory first .

i have an ID column, and a ParentID column.
what i do not know necessarily is the parent ID to start with. so first thing to do would be to find out which node is the parent ID.

what would be best practice to loop over this table and make it hierarchical

BranchId     ParentId    ProductCount    HasBranchChildren       Name
0           NULL        48              1                       Categories
1           0           20              1                       CategoryA
2           1           10              1                       CategoryA1
3           1           8               0                       CategoryA2
4           1           2               0                       CategoryA3
5           2           4               0                       CategoryA1.1
6           2           6               0                       CategoryA1.2
7           0           28              1                       CategoryB
8           7           20              0                       CategoryB1
9           7           8               0                       CategoryB2

this would be an example datatable
of course it will have hundreds of items, and it does not always start at the root node,
its possible that with a request a certain subset of categories is requested,
however i’ve talked with the database team and they will give me NULL on root nodes
in the above example that would be element 0 in the table ….

  • 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-20T07:19:45+00:00Added an answer on May 20, 2026 at 7:19 am

    Step 1.
    You need to create a tree structure from a table in memory from, this should be simple. Checkout this question here:
    Most efficient way of creating tree from adjacency list

    Here is some code for your multi root case (I did not want to modify the parent values to some bogus value, therefore the virtual root and its immediate children will have parent = null, but that doe not matter for traversing the tree, since you have Children and IsRoot):

    public class Branch
    {
        public Branch()
        {
            Children = new List<Branch>();
        }
    
        public bool IsRoot { get; set; }
        public int? BranchId { get; set; }
        public int? ParentId { get; set; }
        public List<Branch> Children { get; set; }
    
        public int ProductCount { get; set; }       
        public int HasBranchChildren { get; set; }
    
        public string Name { get; set; }
        //other data
    }
    
    public class BranchTreeHelper
    {
        public Branch MakeTree() 
        {
            var virtualRoot = new Branch()
            {
                BranchId = null,
                ParentId = null,
                IsRoot = true
            };
    
            // get the data from db, just disregard the properties: Children and IsRoot flage
            List<Branch> branchList = GetDataFromDB();
    
            var branchDict = branchList.ToDictionary(i => i.BranchId.Value, i => i);
    
            foreach(var branch in branchList)
            {
                if(branch.ParentId.HasValue)
                {
                    branchDict[branch.ParentId.Value].Children.Add(branch);
                }
                else
                {
                    virtualRoot.Children.Add(branch);
                }
            }
    
            return virtualRoot;
        }
    }
    

    Step 2.
    Sadly MS made it difficult, by not offering a simple generic way to bind to hierarchical data. But luckily others have created code that can. Use this to bind your new tree structure to a tree view:
    http://ohds.codeplex.com/

    From the project homepage:

    ObjectHierarchicalDataSource is to
    hierarchical data sources what
    ObjectDataSource is to tabular data
    sources. It enables the page developer
    to declaratively bind hierarchical
    controls such as Menu and TreeView to
    almost any object graph.

    You could create you’re own custom implementation of IHierarchicalDataSource but I would strongly advise against that since this is cumbersome and error prone.

    Option 2:
    If you do not want to use third party user controls the checkout this generic implementation of IHierarchicalDataSource this again can be bound to any hierarchical ASP.NET control:
    http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/

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

Sidebar

Related Questions

I have a DataSet which I get a DataTable from that I am being
In C#/ASP.NET 3.5, I have a DataTable which pulls rows from the database. I
I have a DataTable that has a boolean column called [Invalid]. I need to
I have a DataTable which is bound to a GridView. I also have a
I have a datagridview which im binding DataTable to. What I want do is
I have multiple excel files which I open as datatable I want to merge
I have a two-dimensional array (of Strings) which make up my data table (of
I have a DataTable that queries out something like below usergroupid...userid......username 1.............1...........John 1.............2...........Lisa 2.............3...........Nathan
I have a datatable that has a date column, but I am stumped as
I am connecting C# with Oracle 11g. I have a DataTable which i fill

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.