Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Dynamically Binding Values to TreeView Control

0.00/5 (No votes)
24 Jun 2011 1  
First Create Two Tables ParentTable and ChildTableParentTable-->ParentId as Primary Key-->ParentNameChildTable -->ParentId as FOREIGN KEY

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

First Create Two Tables ParentTable and ChildTable

ParentTable

-->ParentId as Primary Key

-->ParentName

ChildTable

-->ParentId as FOREIGN KEY from ParentTable

-->ChildName

-----------------------------------------------------------------------

Create Url SqlConnection...

Then Use This Function to fill TreeView Control


public void fill_Tree()
{
        try
        {
            TreeView1.Nodes.Clear();
            if (con.State == ConnectionState.Closed)
                con.Open();

            SqlCommand SqlCmd = new SqlCommand("Select * from ParentTable", con);
            SqlDataReader Sdr = SqlCmd.ExecuteReader();

            SqlCmd.Dispose();


            string[,] ParentNode = new string[100, 2];

            int count = 0;

            while (Sdr.Read())
            {
                ParentNode[count, 0] = Sdr.GetValue(Sdr.GetOrdinal("ParentID")).ToString();
                ParentNode[count++, 1] = Sdr.GetValue(Sdr.GetOrdinal("ParentName")).ToString();
            }

            Sdr.Close();


            for (int loop = 0; loop < count; loop++)
            {
                TreeNode root = new TreeNode();

                root.Text = ParentNode[loop, 1];
                root.Target = "_blank";
                root.NavigateUrl = "ParentPage.aspx?NodeName=" + root.Text + "";


                SqlCommand Module_SqlCmd = new SqlCommand("Select * from ChildTable where ParentId =" + ParentNode[loop, 0], con);
                SqlDataReader Module_Sdr = Module_SqlCmd.ExecuteReader();

                while (Module_Sdr.Read())
                {
                    TreeNode child = new TreeNode();

                    child.Text = Module_Sdr.GetValue(Module_Sdr.GetOrdinal("ChildName")).ToString();
                    child.Target = "_blank";
                    child.NavigateUrl = "ChildPage.aspx?NodeName=" + child.Text + "";

                    root.ChildNodes.Add(child);
                }

                Module_Sdr.Close();

                TreeView1.Nodes.Add(root);
            }


            TreeView1.ExpandAll();
            con.Close();
        }
        catch (Exception ex)
        {
            this.ErrorState = true;
            this.ErrorMsg = ex.Message;
        }
}

protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack == true)
        {
            fill_Tree();
        }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here