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

前端 未结 3 1023
小鲜肉
小鲜肉 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:08

    Try this may be it can help you.

     protected void Page_Load(object sender, EventArgs e)
        {
            conStr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            conn = new OleDbConnection(conStr);
            BindTreeViewControl();
        }
    
        private void BindTreeViewControl()
        {
            try
            {
                DataSet ds = GetDataSet("Select ProductId,ProductName,ParentId from ProductTable");
                DataRow[] Rows = ds.Tables[0].Select("ParentId = 0"); 
    
                for (int i = 0; i < Rows.Length; i++)
                {
                    TreeNode root = new TreeNode(Rows[i]["ProductName"].ToString(), Rows[i]["ProductId"].ToString());
                    root.SelectAction = TreeNodeSelectAction.Expand;
                    CreateNode(root, ds.Tables[0]);
                    treeviwExample.Nodes.Add(root);
                }
            }
            catch (Exception Ex) { throw Ex; }
        }
    
        public void CreateNode(TreeNode node, DataTable Dt)
        {
            DataRow[] Rows = Dt.Select("ParentId =" + node.Value);
            if (Rows.Length == 0) { return; }
            for (int i = 0; i < Rows.Length; i++)
            {
                TreeNode Childnode = new TreeNode(Rows[i]["ProductName"].ToString(), Rows[i]["ProductId"].ToString());
                Childnode.SelectAction = TreeNodeSelectAction.Expand;
                node.ChildNodes.Add(Childnode);
                CreateNode(Childnode, Dt);
            }
        }
        private DataSet GetDataSet(string Query)
        {
            DataSet Ds = new DataSet();
            try
            {
                OleDbDataAdapter da = new OleDbDataAdapter(Query, conn);
                da.Fill(Ds);
            }
            catch (Exception dex) { }
            return Ds;
        }
    

    and database structure for this is

    enter image description here

    0 讨论(0)
  • 2021-01-23 18:08
    // Suppress repainting the TreeView until all the objects have been created.
    treeView1.BeginUpdate();
    
    // Clear the TreeView each time the method is called.
    treeView1.Nodes.Clear();
    
    // create root node
    TreeNode root = new TreeNode("Root");
    
    // loop and add all child nodes to root node
    foreach (DataRow r in dt.Rows)
    {
        // create child node 
        // add to root node 
         root.Nodes.Add(child);
    }
    
    // add root node to tree view
    treeView1.Nodes.Add(root);
    
    // Begin repainting the TreeView.
    treeView1.EndUpdate();
    
    0 讨论(0)
  • 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<DocumentData> 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!

    0 讨论(0)
提交回复
热议问题