Following is the table structure, and code for creating and filling the n level heirarchical treeview control using Recursion on your web (aspx) page, In my case I used the Component Art grid control, It can be any heirarchical control with Node property.
Here is an assumption that there is a TreeviewControl On your page.
Table Structure
[ID] [Data] [ParentId]
101 Node1 Null
102 Node2 Null
103 Node1_Node1 101
104 Node1_Node2 101
105 Node2_Node1 102
106 Node2_Node2 102
107 Node2_Node1_Node1 102
Function
private void FillData(TreeViewNode parentNode)
{
DataSet ds = new DataSet();SqlConnection cnn = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=Testing;Integrated Security=True");
SqlDataAdapter da;
cnn.Open();
if (parentNode==null)
da = new SqlDataAdapter("Select [ID], [Data], [ParentId] from Treedata Where ParentId IS Null", cnn);
else
da = new SqlDataAdapter("Select [ID], [Data], [ParentId] from Treedata Where ParentId=" + parentNode.ID , cnn);
da.Fill(ds);
Response.Write(ds.Tables[0].Rows.Count);
foreach (DataRow dataRow in ds.Tables[0].Rows)
{
ComponentArt.Web.UI.TreeViewNode node;
node = new ComponentArt.Web.UI.TreeViewNode();
node.ID = dataRow["ID"].ToString();
node.Text = dataRow["Data"].ToString();
if (parentNode==null)
{
TreeView1.Nodes.Add(node);
}
else
{
parentNode.Nodes.Add(node);
}
//Recursive call
FillData(node);
}
//Call the function with null argument
FillData(null);
I believe this will be the good one for you....enjoy