2016-05-13 14 views
0

実行時にTreeViewに階層構造を設定しようとしましたが、私のコードで何か問題が発生しています。TreeView階層が間違って生成される

通常、私はルートノードとして多くのプロジェクトを、サブノードとしてサブプロジェクトを続けるべきです。

プロジェクト1,9,10,62、および65がルーツまたはプロジェクトであるとします。

  • プロジェクト#1サブプロジェクト17、24、33、34、35、61、98が含まれている、...
  • プロジェクト#9には、サブプロジェクトを含みません。
  • プロジェクト#10は2と104
  • プロジェクト#62は63、64、108、109
  • プロジェクト#65が再び他のサブプロジェクトを持つサブプロジェクトが含まれているサブプロジェクトを含んでいます。

enter image description here

問題:コードが互いにルートノードを追加し続けます。したがって、次のルートノードは、前のルートノードの子ノードとみなされます。

結果:コードでは、サブプロジェクト(サブプロジェクトにはサブを含むこともできます)とは別のルートノードを作成する必要があります。

コード:

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: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

サンプルデータを提供してください - '一覧 LST =新しいリスト {...};' –

+0

空白リスト内の新しい項目を意味します。 – SamekaTV

+0

サンプルデータは、必要な結果を得るために解析できません。入力データには、親と子/子が必要です。 "1"には親がありません。 "1"の子は "9"、 "17"、 "33"、 "110"、 "111"、 "128"です。 "9"は "10"の親です。 "10"は "62"の親です。 "62"は "65"の親です。 「65」は、「63」、「64」、「108」、「109」の親である。 – jdweng

答えて

1

内側ループを入力する前に、変数をにリセットする必要があります。そして、より良い彼らが必要とされる場所で変数を宣言して初期化し、混乱と同様のエラーを回避するために:

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]; 
     } 
    } 
} 
関連する問題