2017-01-26 35 views
0

我的第一個TreeView的掙扎後,它幾乎工作。我的問題是,在Click事件的代碼底部,根節點似乎是集合中的唯一節點,這使得我無法在檢查根節點時檢查所有節點。我不知道我是怎麼做的,儘管我懷疑我可能錯誤地添加了節點。 樹中有一個根節點和12個parentNode節點。每個parentNode有多個secondChild節點。每個secondChild節點都有多個thirdChild節點。這是一個Windows窗體。所有的代碼在這裏列出。任何幫助表示讚賞。的childNodes不在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