How To Add Root Node In Tree View Dynamically Using Asp.net

前端 未结 3 1025
小鲜肉
小鲜肉 2021-01-23 17:53

I Want To Bind DataTable To TreeView.I Have Written Following Code.Its Working Currently,Means It Displays All Data Of DataTable But No Root Node.

 List

        
3条回答
  •  情话喂你
    2021-01-23 18:33

    Did you ever get this answered? You were almost there.

    What is the name of your TreeView control? Since you never said, I am using treeView1, and modified your code to include that below:

    private TreeView treeView1;
    
    private void TreeView_DataBind() {
    
      treeView1.Nodes.Clear();
    
      List lstData = GetSPDocuments();
      gvDocuments.DataSource = lstData;
      gvDocuments.DataBind();
    
      DataTable dt = ConvertToDataTable(lstData);
    
      TreeNode node1 = new TreeNode("Root");
    
      treeView1.Nodes.Add(node1); // this is the step you missed
    
      foreach (DataRow r in dt.Rows)
      {
        int nodeLvl = int.Parse(r["ID"].ToString());
        string nodeParent = "Folders";
        string nodeName = r["Title"].ToString();
    
        TreeNode tNode = new TreeNode(nodeName);
    
        ht.Add(nodeLvl.ToString() + nodeName, tNode);
    
        if (tvDocs.Nodes.Count == 0)
          tvDocs.Nodes.Add(tNode);
        else
        {
          nodeLvl--;
          tvDocs.Nodes.Add(tNode);               
        }
      }
    
      node1.Expand();
    
    }
    

    Easy peasy!

提交回复
热议问题