none
ASP.NET Problem with TreeView bind from datasource RRS feed

  • Question

  • Hi

    I am trying to populate TreeView from database but I am having problems with sub child nodes 

    Why "Durres" doesnt show up as child of "Tirana" and why "Shkoder" doesnt show up as child of "Durres"?

    Database code

    CREATE TABLE [dbo].[TopBill] (
        [srNo]     INT          NOT NULL IDENTITY,
        [Pid]      INT          NULL,
        [PName]    VARCHAR (50) NULL,
        [PDetails] NCHAR (10)   DEFAULT (NULL) NULL,
        [cId]      INT          NULL,
        [Cname]    VARCHAR (50) NULL,
        [Cqty]     INT          DEFAULT (NULL) NULL,
        CONSTRAINT [PK_TopBill] PRIMARY KEY CLUSTERED ([srNo] ASC)
    );

    Database data

    C# code

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebApplication1
    {
        public partial class Index : System.Web.UI.Page
        {
            Boolean m_bNodeFound;
            Boolean intNodeFound;
            string sValuepath;
            protected void Page_Load(object sender, EventArgs e)
            {
    
    
    
    
                if (!IsPostBack)
                {
    
                    using (var connection = new SqlConnection("Data Source=(localdb)\\ProjectsV13;Initial Catalog=gab;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"))
                    {
                        string com = "Select * from dbo.TopBill";
                        SqlDataAdapter adpt = new SqlDataAdapter(com, connection);
                        DataTable dt = new DataTable();
                        adpt.Fill(dt);
                        connection.Open();
                        TreeNode root = new TreeNode();
    
                        root = new TreeNode();
                        root.Text = dt.Rows[0][5].ToString();
                        root.Value = dt.Rows[0][5].ToString();
                        TreeView1.Nodes.Add(root);
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            //make NodeNotFound  at each row
                            m_bNodeFound = false;
    
                            //root as it is
                            TreeNode tn = root;
    
                            //get NodeName by cName
                            string nodeName = dt.Rows[i][5].ToString();
    
                            //Check if it is not null, to skip errors
                            if (nodeName != null && nodeName != "")
                            {
                                //MainNodecollections
                                TreeNodeCollection nodes = TreeView1.Nodes;
    
                                //Check if node already exists in Treeview
                                FindNodeInHierarchy(nodeName);
    
                                //If not found then continue
                                if (!m_bNodeFound)
                                {
                                    //If node is root node
                                    if (dt.Rows[i][2].ToString() == "" || dt.Rows[i][2].ToString() == null)
                                    {
                                        TreeNode root2 = new TreeNode();
                                        root2.Text = dt.Rows[i][5].ToString();
                                        root2.Value = dt.Rows[i][5].ToString();
                                        TreeView1.Nodes.Add(root2);
                                    }
                                    //Check if node is child node
                                    else if (CheckRowsAllValues(dt.Rows[i][1].ToString(), dt))
                                    {
                                        //Find New Root of child node
                                        TreeNode NewRoot = FindRoot(dt.Rows[i][1].ToString(), dt, root);
                                        //if New root is not empty 
                                        if (NewRoot != null)
                                        {
                                            TreeNode root2 = new TreeNode();
                                            root2.Text = dt.Rows[i][5].ToString();
                                            root2.Value = dt.Rows[i][5].ToString();
                                            //append child node(current value) with new root
                                            NewRoot.ChildNodes.Add(root2);
                                        }
    
                                    }
                                }
    
                            }
                        }
    
                    }
                }
            }
    
            private TreeNode FindRoot(string v, DataTable dt, TreeNode tNode)
            {
                string expression = "cId = " + v;
                DataRow[] foundRows;
                foundRows = dt.Select(expression);
    
                //Find node using Id of table row
                TreeNode tn = TreeView1.FindNode(foundRows[0][0].ToString());
    
                //if not found, search using Name
                if (tn == null && foundRows[0][5].ToString() != "")
                {
                    var value = foundRows[0][5].ToString();
                    TreeNode searchedNode = null;
    
                    //search node by Value(City Name) by looping each node
                    foreach (TreeNode node in TreeView1.Nodes)
                    {
                        if (searchedNode == null)
                        {
                            searchedNode = SearchNode(node, value);
                            if (searchedNode == null)
                            {
                                foreach (TreeNode childNode in node.ChildNodes)
                                {
                                    searchedNode = SearchNode(childNode, value);
                                    if (searchedNode != null)
                                        tn = searchedNode;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    tn = searchedNode;
    
                }
    
                return tn;
            }
    
            //Search Node code
            private TreeNode SearchNode(TreeNode node, string searchText = null)
            {
                if (node.Text == searchText) return node;
    
                TreeNode tn = null;
                foreach (TreeNode childNode in node.ChildNodes)
                {
                    tn = SearchNode(childNode);
                    if (tn != null) break;
                }
    
                if (tn != null) node.Expand();
                return tn;
            }
    
            private bool CheckRowsAllValues(string v, DataTable dt)
            {
                string expression = "cId = " + v;
                DataRow[] foundRows;
                foundRows = dt.Select(expression);
                if (foundRows.Count() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
    
    
    
            private void FindNodeByValue(string strValue)
            {
                // If the TreeView control contains any root nodes, perform a
                // preorder traversal of the tree and display the text of each node.
                if (TreeView1.Nodes.Count > 0)
                {
    
                    // Iterate through the root nodes in the Nodes property.
                    for (int i = 0; i < TreeView1.Nodes.Count; i++)
                    {
    
                        // Display the nodes.
                        DisplayChildNodeText(TreeView1.Nodes[i], strValue);
    
                    }
    
                }
            }
    
    
            void DisplayChildNodeText(TreeNode node, string strValue)
            {
    
                // Display the node's text value.
                //Message.Text += node.Text + "<br />";
                if (strValue == node.Text.ToString())
                {
                    sValuepath = node.ValuePath;
                    intNodeFound = true;
                }
    
                // Iterate through the child nodes of the parent node passed into
                // this method and display their values.
                for (int i = 0; i < node.ChildNodes.Count; i++)
                {
    
                    DisplayChildNodeText(node.ChildNodes[i], strValue);
    
                }
    
                if (intNodeFound) return;
    
            }
    
    
    
            private void FindNodeInHierarchy(string strSearchValue)
            {
    
                // If the TreeView control contains any root nodes, perform a
                // preorder traversal of the tree and display the text of each node.
                if (TreeView1.Nodes.Count > 0)
                {
    
                    // Iterate through the root nodes in the Nodes property.
                    for (int i = 0; i < TreeView1.Nodes.Count; i++)
                    {
    
                        // Display the nodes.
                        CheckChildNodeText(TreeView1.Nodes[i], strSearchValue);
    
                    }
    
                }
            }
    
    
            void CheckChildNodeText(TreeNode node, string strValue)
            {
    
                // Display the node's text value.
                //Message.Text += node.Text + "<br />";
                if (strValue == node.Text.ToString())
                {
                    m_bNodeFound = true;
                }
    
                // Iterate through the child nodes of the parent node passed into
                // this method and display their values.
                for (int i = 0; i < node.ChildNodes.Count; i++)
                {
    
                    DisplayChildNodeText(node.ChildNodes[i], strValue);
    
                }
    
                if (m_bNodeFound) return;
    
            }
        }
    }


    The result 

    What code should do or what I want in this case

    Wednesday, December 11, 2019 9:47 PM

Answers

  • Hi ai1231,

    Thank you for posting here.

    According to your requirements, for treeview's data binding, I suggest that you can create a custom method and use the recursive implementation, which will be more convenient and simple.

    For your current treeview, you can refer to the following code to implement:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = this.GetData("SELECT * FROM TopBill where Pid =0");
                    AddNodes(dt, 0, null);
    
                }
            }
            private void AddNodes(DataTable dt, int parentId, TreeNode node)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    TreeNode subnode = new TreeNode
                    {
                        Text = dr["Cname"].ToString(),
                        Value = dr["cId"].ToString()
                    };
                    DataTable subdt = this.GetData("SELECT * FROM TopBill where Pid = " + subnode.Value.ToString());
                    if (parentId == 0)
                    {
                        TreeView1.Nodes.Add(subnode);
                        AddNodes(subdt, int.Parse(subnode.Value.ToString()), subnode);
                    }
                    else
                    {
                        AddNodes(subdt, int.Parse(subnode.Value.ToString()), subnode);
                        node.ChildNodes.Add(subnode);
                    }
                    
                }
    
    
            }
            private DataTable GetData(string query)
            {
                DataTable dt = new DataTable();
                string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand(query))
                    {
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.CommandType = CommandType.Text;
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            sda.Fill(dt);
                        }
                    }
                    return dt;
                }
            }   
                         
    


    You can also refer to these links :

    Populate (Bind) TreeView from database in ASP.Net

    Loading a treeview to n levels

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 13, 2019 7:10 AM

All replies

  • ASP.NET issues can be discussed at the ASP.NET forums.

    http://forums.asp.net/

    Wednesday, December 11, 2019 10:00 PM
  • Hi ai1231,

    Thank you for posting here.

    According to your requirements, for treeview's data binding, I suggest that you can create a custom method and use the recursive implementation, which will be more convenient and simple.

    For your current treeview, you can refer to the following code to implement:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = this.GetData("SELECT * FROM TopBill where Pid =0");
                    AddNodes(dt, 0, null);
    
                }
            }
            private void AddNodes(DataTable dt, int parentId, TreeNode node)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    TreeNode subnode = new TreeNode
                    {
                        Text = dr["Cname"].ToString(),
                        Value = dr["cId"].ToString()
                    };
                    DataTable subdt = this.GetData("SELECT * FROM TopBill where Pid = " + subnode.Value.ToString());
                    if (parentId == 0)
                    {
                        TreeView1.Nodes.Add(subnode);
                        AddNodes(subdt, int.Parse(subnode.Value.ToString()), subnode);
                    }
                    else
                    {
                        AddNodes(subdt, int.Parse(subnode.Value.ToString()), subnode);
                        node.ChildNodes.Add(subnode);
                    }
                    
                }
    
    
            }
            private DataTable GetData(string query)
            {
                DataTable dt = new DataTable();
                string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand(query))
                    {
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.CommandType = CommandType.Text;
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            sda.Fill(dt);
                        }
                    }
                    return dt;
                }
            }   
                         
    


    You can also refer to these links :

    Populate (Bind) TreeView from database in ASP.Net

    Loading a treeview to n levels

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 13, 2019 7:10 AM