将子节点添加到树视图

时间:2016-05-02 07:43:26

标签: c# treeview

ProjectNumbers = DatabaseHandler.Instance.QuerySingleColumn(SelectProjectNumberQuery, "ProjectNumber");
      foreach (string ProjectNumber in ProjectNumbers)
      {
             ProjectTree.Nodes.Add(ProjectNumber);
             ProjectTree.EndUpdate();
             SelectWorkOrderNumberQuery = "SELECT DISTINCT WorkOrderNumber FROM dbo.SUBPRODUCTS WHERE ProjectNumber =" + Int32.Parse(ProjectNumber) + ";";
             WorkOrderNumbers = DatabaseHandler.Instance.QuerySingleColumn(SelectWorkOrderNumberQuery, "WorkOrderNumber");
                  foreach(string WorkOrderNumber in WorkOrderNumbers)
                  {                 
                      ProjectTree.Nodes[ProjectNumber].Nodes.Add(WorkOrderNumber); 
                  }
      }

我正在尝试使用SQL数据库中的ProjectNumbers和WorkOrderNumbers来填充ProjectTree。 ProjectTree.Nodes.Add(ProjectNumber)在第一个foreach循环中正常工作。

当我将WorkOrderNumber添加为子项(第二个foreach循环)时,我得到的对象没有引用错误,无法弄清楚原因。找到了一些使用SelectedNode()函数的相关主题,但这对我不起作用。

1 个答案:

答案 0 :(得分:0)

让我们试试这个:

 ProjectNumbers = DatabaseHandler.Instance.QuerySingleColumn(SelectProjectNumberQuery, "ProjectNumber");
          foreach (string ProjectNumber in ProjectNumbers)
          {
                                 var node = new TreeNode(ProjectNumber);

                 SelectWorkOrderNumberQuery = "SELECT DISTINCT WorkOrderNumber FROM dbo.SUBPRODUCTS WHERE ProjectNumber =" + Int32.Parse(ProjectNumber) + ";";
                 WorkOrderNumbers = DatabaseHandler.Instance.QuerySingleColumn(SelectWorkOrderNumberQuery, "WorkOrderNumber");
                      foreach(string WorkOrderNumber in WorkOrderNumbers)
                      {                 
                            var workNode = new TreeNode(WorkOrderNumber);
                    node.Nodes.Add(workNode);
                      }
 ProjectTree.Nodes.Add(node);
          }