2016-05-13 12 views
0

Ich versuche, ein TreeView zur Laufzeit hierarchisch zu füllen, aber etwas passiert in meinem Code falsch.Die TreeView-Hierarchie wird falsch generiert

Normalerweise sollte ich viele Projekte als Wurzelknoten haben, gefolgt von Unterprojekten als Unterknoten und so weiter.

Nehmen wir an, die Projekte 1, 9, 10, 62 und 65 sind die Wurzeln oder Projekte.

  • Projekt # 1 enthält Teilprojekte 17, 24, 33, 34, 35, 61, 98, ...
  • Projekt # 9 keine Unter Projekte enthält.
  • Projekt # 10 enthält Teilprojekte 2 und 104
  • Projekt # 62 enthält Teilprojekte 63, 64, 108, 109.
  • Projekt # 65 hat wiederum andere Teilprojekte.

enter image description here

Problem: Der Code hält zueinander Wurzelknoten hinzugefügt wird. Daher betrachtet es den nächsten Stammknoten als Kind des vorherigen.

Ergebnis: Der Code sollte getrennte Wurzelknoten mit Unterprojekten erstellen (Unterprojekte können auch Unterobjekte enthalten).

Code:

private void button2_Click(object sender, EventArgs e) 
{ 
    DbConnector db = new DbConnector(); 
    string str = ""; 
    List<string> lst = db.ReadProjectsTable(); 
    lst.OrderBy(x => x.Count(y => y == '|')); 

    List<string> newLst = new List<string>(); 
    foreach (var item in lst) 
    { 
     string output = ""; 
     foreach (var item2 in item.Split('|', '|')) 
     { 
      output += item2 + '-'; 
     } 
     output = output.Substring(output.IndexOf('-')+1, output.LastIndexOf('-')-2); 
     newLst.Add(output); 
     str += output + Environment.NewLine; 
    } 
    textBox2.Text = str; 

    tvProjects.PathSeparator = @"-"; 

    PopulateTreeView(tvProjects, newLst, '-'); 
} 

private static void PopulateTreeView(TreeView treeView, IEnumerable<string> paths, char pathSeparator) 
{ 
    TreeNode lastNode = null; 
    string subPathAgg; 
    foreach (string path in paths) 
    { 
     subPathAgg = string.Empty; 
     foreach (string subPath in path.Split(pathSeparator)) 
     { 
      subPathAgg += subPath + pathSeparator; 
      TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true); 
      if (nodes.Length == 0) 
       if (lastNode == null) 
        lastNode = treeView.Nodes.Add(subPathAgg, subPath); 
       else 
        lastNode = lastNode.Nodes.Add(subPathAgg, subPath); 
      else 
       lastNode = nodes[0]; 
     } 
    } 
} 

UPDATE: Beispieldaten von List<string> lst

| 1 | | 9 | | 10 | | 62 | | 65 | | 67 | | 78 | | 83 | | 86 | | 105 | | 116 | | 125 | | 10 | 2 | | 67 | 4 | | 1 | 17 | | 1 | 24 | | 1 | 33 | | 1 | 34 | | 1 | 35 | | 1 | 61 | | 62 | 63 | | 62 | 64 | | 67 | 68 | | 65 | 69 | | 65 | 70 | | 65 | 71 | | 65 | 72 | | 65 | 75 |

+0

Bitte geben Sie die Datenprobe - 'Liste lst = new List {...};' –

+0

Leer bedeutet ein neues Element in der Liste – SamekaTV

+0

Ihre Beispieldaten können nicht analysiert werden, um die gewünschten Ergebnisse zu erzielen. Eingabedaten sollten Eltern und Kind/Kinder haben. "1" hat kein Elternteil. Die Kinder von "1" sind: "9", "17", "33", "110", "111", "128". "9" ist das Elternteil von "10". "10" ist das übergeordnete Element von "62". "62" ist das übergeordnete Element von "65". "65" ist das Elternteil von "63", "64", "108", "109". – jdweng

Antwort

1

sollten Sie setzen das lastNode Variable null bevor die innere Schleife eintritt. Und Verwirrung und ähnliche Fehler zu vermeiden, besser erklären und initialisieren Variablen an der Stelle, wo sie gebraucht werden:

private static void PopulateTreeView(TreeView treeView, IEnumerable<string> paths, char pathSeparator) 
{ 
    foreach (string path in paths) 
    { 
     string subPathAgg = string.Empty; 
     TreeNode lastNode = null; 
     foreach (string subPath in path.Split(pathSeparator)) 
     { 
      subPathAgg += subPath + pathSeparator; 
      TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true); 
      if (nodes.Length == 0) 
       if (lastNode == null) 
        lastNode = treeView.Nodes.Add(subPathAgg, subPath); 
       else 
        lastNode = lastNode.Nodes.Add(subPathAgg, subPath); 
      else 
       lastNode = nodes[0]; 
     } 
    } 
} 
Verwandte Themen