In the database there is a table that stores the data tree, and has something like the following: id, tree, name

Those. Record id, tree indicates the belonging to this or that node, and the name itself - i.e. node name

So, the tree has entries of the form:

1. 1.1. 1.1.1 1.1.2 1.2. 1.2 1.2.1 

I would like to parse all this and display it in the TreeView, but for now I don’t know how to even approach this task (I suspect that there is some simple solution, and there is no need to reinvent the wheel

  • one
    What have you tried, what code did you write that didn't work out? - Kromster
  • Honestly, this is just an acquaintance with c #. Before that there was an experience with php. While only learning, so please do not kick much. - Alexey
  • 2
    To begin with, the answer depends on your chosen UI framework. In C #, there is no TreeView at all. - VladD
  • @VladD, by default, winforms is implied, and wpf should be called explicitly? - Qwertiy ♦
  • @Qwertiy: Whom like. For example, I consider WinForms an obsolete anachronism, which even has no layout manager. And someone, on the contrary, considers WPF to be a creepy slow-down monster with unreadable XML in syntax and the impossibility of normal debugging. And someone generally thinks that the era of fat clients has passed, and we need to output data to the Web, so the only normal framework is ASP.NET. And still somebody considers everything, except GTK #, to stand, for it is not cross-platform. In general, tastes differ. - VladD pm

2 answers 2

 using System; using System.Collections.Generic; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { List<Header> list = new List<Header>() { new Header() { Name = "1", List = new List<Header>() { new Header() { Name = "1.1", List = new List<Header>() { new Header() { Name = "1.1.1"}, new Header() { Name = "1.1.2"}, new Header() { Name = "1.1.3"}, } }, new Header() { Name = "1.2"}, new Header() { Name = "1.3"}, } }, new Header() { Name = "2", List = new List<Header>() { new Header() { Name = "2.1", List = new List<Header>() { new Header() { Name = "2.1.1"}, new Header() { Name = "2.1.2"}, new Header() { Name = "2.1.3"}, } }, new Header() { Name = "2.2"}, new Header() { Name = "2.3"}, }} }; treeView1.Nodes.Add("Headers"); BuildTreeView(list, treeView1.Nodes[0]); } private void BuildTreeView(List<Header> list, TreeNode parentNode) { if (list.Count != 0) { foreach (var item in list) { TreeNode tn = new TreeNode(item.Name); parentNode.Nodes.Add(tn); if (item.List != null) { BuildTreeView(item.List, tn); } } } } } sealed class Header { public string Name { get; set; } public List<Header> List { get; set; } } } 

Actually, if you already have a data structure, you can simply use the method to build the tree.

My example deduces so:

enter image description here

    Dictionary<TKey, TValue> everything in Dictionary<TKey, TValue> with id as the key and the whole record as the value. You pass through foreach through it and add children to the parent element, and add all entries with an empty parent to the list. The result will be a forest.