2017-01-26 4 views
0

最初のTreeViewに苦労した後、ほとんど動作します。私の問題は、Clickイベントのコードの一番下に、ルートノードがコレクションの唯一のノードであるように見えて、ルートノードがチェックされたときにすべてのノードをチェックできなくなってしまうことです。私はおそらくノードを間違って追加したと思われるが、これを引き起こすために何をしたのか分からない。 ツリーにはルートノードと12個のparentNodeノードがあります。各parentNodeには複数のsecondChildノードがあります。各secondChildノードには複数のthirdChildノードがあります。これはWindowsフォームです。すべてのコードがここにリストされています。どんな助けもありがとうございます。子ノードはTreeViewコレクションにありません

public Form1() 
    { 
     InitializeComponent(); 

     FillTreeParentNodes(); 
    } 

    public void FillTreeParentNodes() 
    { 
     connect.Open(); 

     DataTable dtGroups = new DataTable("FirstChildNodes"); 

     DataSet dsNodes = new DataSet(); 
     dsNodes.Tables.Add(dtGroups); 

     SqlDataAdapter daAdapter = new SqlDataAdapter("RMM3DMTVColorDesc", connect); 
     daAdapter.SelectCommand.CommandType = CommandType.StoredProcedure; 
     daAdapter.Fill(dsNodes, "FirstChildNodes"); 
     daAdapter.Dispose(); 

     tvDiscountMaintenance.Nodes.Clear(); 

     if (dsNodes.Tables[0].Rows.Count > 0) 
     { 
      TreeNode root = new TreeNode("Select All"); 
      tvDiscountMaintenance.Nodes.Add(root); 

      foreach (DataRow row in dsNodes.Tables[0].Rows) 
      { 

       TreeNode parentNode = new TreeNode(row["DisColorDesc"].ToString()); 
       parentNode.Text = row["DisColorDesc"].ToString(); 
       root.Nodes.Add(parentNode); 

       FillChildNodes(parentNode, root); 
      } 
     } 
    } 

     private void FillChildNodes(TreeNode parentNode, TreeNode root) 
    { 

     DataTable dtSecondGroup = new DataTable("SecondChildNodes"); 

     DataSet dsCNodes = new DataSet(); 
     dsCNodes.Tables.Add(dtSecondGroup); 

     SqlDataAdapter daAdapter = new SqlDataAdapter("RMM3DMTVColorCodes", connect); 
     daAdapter.SelectCommand.CommandType = CommandType.StoredProcedure; 
     daAdapter.Fill(dsCNodes, "SecondChildNodes"); 
     daAdapter.Dispose(); 

     if (dsCNodes.Tables[0].Rows.Count > 0) 
     { 
      foreach (DataRow chRow in dsCNodes.Tables[0].Rows) 
      { 
       if (parentNode.Text.Equals(chRow["DisColorDesc"].ToString())) 
       { 
        TreeNode secondChild = new TreeNode(); 
        secondChild.Text = chRow["DisColorCode"].ToString(); 
        parentNode.Nodes.Add(secondChild); 
        FillThirdChildNodes(secondChild, root); 
       } 
      } 
     } 

    } 

    private void FillThirdChildNodes(TreeNode secondChild, TreeNode root) 
    { 
     DataTable dtThirdGroup = new DataTable("ThirdChildNodes"); 

     DataSet dsThNodes = new DataSet(); 
     dsThNodes.Tables.Add(dtThirdGroup); 

     SqlDataAdapter daAdapter = new SqlDataAdapter("RMM3DMTVCategories", connect); 
     daAdapter.SelectCommand.CommandType = CommandType.StoredProcedure; 
     daAdapter.Fill(dsThNodes, "ThirdChildNodes"); 
     daAdapter.Dispose(); 

     if (dsThNodes.Tables[0].Rows.Count > 0) 
     { 
      foreach (DataRow chRow in dsThNodes.Tables[0].Rows) 
      { 
       if (secondChild.Text.Equals(chRow["DisColorCode"].ToString())) 
       { 
        TreeNode thirdChild = new TreeNode(); 
        thirdChild.Text = chRow["DisCategoryDesc"].ToString; 
        secondChild.Nodes.Add(thirdChild); 
       } 
      } 
     } 
     connect.Close(); 
    } 

    private void tvDiscountMaintenance_Click(object sender, EventArgs e) 
    { 
     if (tvDiscountMaintenance.Nodes.Count > 0) // I think this is telling me that the root node is the only one in the Collection. 
     { 
      if (tvDiscountMaintenance.TopNode.Checked) 
      { 
       foreach (TreeNode node in tvDiscountMaintenance.Nodes) 
       { 
       node.ExpandAll(); 
       node.Checked = true; 
       } 
      } 
     } 
    } 
+0

に確認することができます。再帰的に行われたベスト。 – TaW

+0

ありがとう。前の質問や例が分かっていますか?私はここで私の要素の方法です。 – mengli00

+0

[はい。 TreeViewですべてのノードを収集する関数については、ここを参照](http://stackoverflow.com/questions/26687906/find-only-child-nodes-in-treeview-c-sharp/26687949?s=6|1.8069# 26687949) – TaW

答えて

0

ツリービューのノードコレクションが唯一のあなたがそうあなたがノードツリーを通過しなければならない存在するどのように多くのノード全体で見て、他のノードに子ノードを追加している、存在するどのように多くのノードこのコレクションでわかります。

あなたは、ルートノードの内部だけのノードを確認したい場合は、あなたがそのすべてのノードを見つけ、各ノードから移動する必要がありますtvDiscountMaintenance.Nodes[0].Nodes

関連する問題