Click here to Skip to main content
15,881,801 members
Articles / Programming Languages / C#

Populate TreeView from Simple "list"

Rate me:
Please Sign up or sign in to vote.
4.40/5 (4 votes)
27 Aug 2012CPOL1 min read 58.9K   1.5K   8  
How to SIMPLY populate TreeView from some sort of "list" variable / object / structure
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace TreeViewExample
{
   public partial class Form1 : Form
   {
      public class kData {   // instead of class you can use struct, but with some dificulties
         public int index;
         public int level;
         public string NodeText;
         public string OtherData;
         //public stData()  { } // here you can initialize variables
      };
      List<kData> L; // instead of list you can use array, but I prefer Lists

      public Form1() {
         InitializeComponent();
      }

      private void btnPopulate_Click(object sender, EventArgs e) {
         // take data from ListBox and store it to List
         string line;
         L = new List<kData>();
         for (int i = 0; i < lbData.Items.Count; i++)  {
            line = lbData.Items[i].ToString();
            L.Add(new kData());
            L[i].index = i;
            L[i].level = Convert.ToInt32(line.Substring(0,1));
            L[i].NodeText = line.Substring(2);
            L[i].OtherData = "some other data you might need";
         }

         populateBaseNodes();
      }

      public void populateBaseNodes()
      {
         int i;
         tvData.Nodes.Clear();
         tvData.BeginUpdate();

         for (i = 0; i < L.Count(); i++) {
            if (L[i].level == 0) {
               tvData.Nodes.Add(L[i].NodeText, L[i].NodeText);
               tvData.Nodes[tvData.Nodes.Count - 1].Tag = L[i];
            }
         }

         for (i = 0; i < tvData.Nodes.Count; i++)
            populateChilds(tvData.Nodes[i]);

         tvData.EndUpdate();
         tvData.Refresh();
      }

      public void populateChilds(TreeNode parentNode)  
      {
         kData parentRed = (kData)parentNode.Tag;

         for (int i = parentRed.index + 1; i < L.Count; i++) {
            if (L[i].level == (parentRed.level + 1)) {
               parentNode.Nodes.Add(L[i].NodeText, L[i].NodeText);
               parentNode.Nodes[parentNode.Nodes.Count - 1].Tag = L[i];
               populateChilds(parentNode.Nodes[parentNode.Nodes.Count - 1]);
            }
            if (L[i].level <= parentRed.level) break;
         }
      }
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Yugoslavia Yugoslavia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions