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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:45:10+00:00 2026-06-14T06:45:10+00:00

I have a datatable loaded from a table of a database that contains elements

  • 0

I have a datatable loaded from a table of a database that contains elements to be loaded into a treeView.

The database table looks like the following :

+--------+---------+-------------------------------+
|liv     | cod     | des                           |
|--------+---------+-------------------------------+
| 1      | AAA     |    PANE E SOSTITUTIVI     |
| 2      | AAAA    |GRISSINI - CRACKERS E SIMIL    |
| 3      | AAAA    |    MAIS                   |
| 4      | AAAA B  |    BIANCO                 |
| 4      | AAAA I  |    INTEGRALE              |
| 3      | AAAAA   |    AVENA                  |
| 4      | AAAAAB  |    BIANCO                 |
| 4      | AAAAAI  |    INTEGRALE              |
| 3      | AAAAB   |    ALTRO                  |
| 4      | AAAABB  |    BIANCO                 |
| 4      | AAAABI  |    INTEGRALE              |
| 3      | AAAAF   |    FARRO                  |
| 4      | AAAAFB  |    BIANCO                 |
| 4      | AAAAFI  |    INTEGRALE              |
| 3      | AAAAK   |    KAMUT                  |
| 4      | AAAAKB  |    BIANCO                 |
| 4      | AAAAKI  |    INTEGRALE              |
| 3      | AAAAR   |    FRUMENTO               |
| 4      | AAAARB  |    BIANCO                 |
| 4      | AAAARI  |    INTEGRALE              |
| 3      | AAAAS   |    RISO                   |
| 4      | AAAASB  |    BIANCO                 |
| 4      | AAAASI  |    INTEGRALE              |
| 2      | AAAC    |    ESTRUSI                |
| 3      | AAACA   |    MAIS               |
+--------+---------+-------------------------------+

Since the maximum level (liv) of the tree is 5, here it is the function that I’m using at the moment to load the datatable into the TreeView :

   public void loadFromDataTable(DataTable table, TreeView tree) {

        DataView view1 = new DataView(table);
        view1.RowFilter = "liv = 1";

            foreach (DataRowView dr in view1) {
                TreeNode root = new TreeNode(dr["des"].ToString());
                DataView view2 = new DataView(table);
                view2.RowFilter = "liv = 2 AND cod LIKE '" + dr["cod"].ToString().Trim() + "%'";

                foreach (DataRowView dr2 in view2) {
                    TreeNode root2 = new TreeNode(dr2["des"].ToString());

                    DataView view3 = new DataView(table);
                    view3.RowFilter = "liv = 3 AND cod LIKE '" + dr2["cod"].ToString().Trim() + "%'";

                    foreach (DataRowView dr3 in view3) {
                        TreeNode root3 = new TreeNode(dr3["des"].ToString());

                        DataView view4 = new DataView(table);
                        view4.RowFilter = "liv = 4 AND cod LIKE '" + dr3["cod"].ToString().Trim() + "%'";

                        foreach (DataRowView dr4 in view4) {
                            TreeNode root4 = new TreeNode(dr4["des"].ToString());

                            DataView view5 = new DataView(table);
                            view5.RowFilter = "liv = 5 AND cod LIKE '" + dr4["cod"].ToString().Trim() + "%'";

                            foreach (DataRowView dr5 in view5) {
                                TreeNode root5 = new TreeNode(dr5["des"].ToString());

                                root4.Nodes.Add(root5);
                            }

                            root3.Nodes.Add(root4);
                        }

                        root2.Nodes.Add(root3);
                    }

                    root.Nodes.Add(root2);
                }
                tree.Nodes.Add(root);
            }

I was wondering if there is a better approach to avoid the use of these five nested loops . Any object oriented approach ?

Thanks for the help.

  • 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-14T06:45:11+00:00Added an answer on June 14, 2026 at 6:45 am

    You can use recursion for adding nodes:

    private TreeNode[] GetNodes(DataTable table, int level = 1, string code = "")
    {
        return table.AsEnumerable()
                    .Where(row => row.Field<int>("liv") == level
                                  && row.Field<string>("cod").StartsWith(code))
                    .Select(row =>
                    {
                        var node = new TreeNode(row.Field<string>("des"));
                        node.Nodes.AddRange(GetNodes(table, level + 1, row.Field<string>("cod")));
                        return node;
                    })
                    .ToArray();
    }
    

    And usage is very simple:

    treeView.Nodes.AddRange(GetNodes(table));
    

    Also this code will work for variable tree depth – it goes deeper only if nodes found on current level. BTW if datatable is large, then consider to do lazy loading of child nodes. In this case you also can use method above, but without recursive call.

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

Sidebar

Related Questions

hope you are doing fine. I have a datatable loaded from a database. I
I have a DataTable that I manually created and loaded with data using C#.
I have a Datatable in my C# program that I would like to INSERT
I Loaded Data From Datatable to DatagridView which look like this: SerialNumber1 | Quantity
I have the following situation: there is a table in the DB that is
I have a data table I've loaded from a CSV file. I need to
I have some DataTables loaded into memory from various sources (SQL and MS Access),
I have loaded data from the database to the dataset named myDS . myDS
Currently I have an application that takes information from a SQLite database and puts
Seeking help from friends I have loaded 3 tables from database to DataSet using

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.