The Example at
Populate TreeView From DataBase[
^] by
Moinul Islam[
^] is working just fine,I just checked it.. This is the modified code according to my local table that i used..
public SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
Load_tree();
}
protected DataSet PDataset(string select_statement)
{
Connect();
con.Open();
SqlDataAdapter ad = new SqlDataAdapter(select_statement, con);
DataSet ds = new DataSet();
ad.Fill(ds);
con.Close();
return ds;
}
public void Connect()
{
con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["cnstring"].ConnectionString);
}
public void Load_tree()
{
DataSet PrSet = PDataset("SELECT * FROM tbl_WebMenu");
TreeView1.Nodes.Clear();
foreach (DataRow dr in PrSet.Tables[0].Rows)
{
if ((int)dr["ParentID"] == 0)
{
TreeNode tnParent = new TreeNode();
tnParent.Text = dr["MenuName"].ToString();
string value = dr["MenuID"].ToString();
tnParent.Expand();
TreeView1.Nodes.Add(tnParent);
FillChild(tnParent, value);
}
}
}
public int FillChild(TreeNode parent, string IID)
{
DataSet ds = PDataset("SELECT * FROM tbl_WebMenu WHERE ParentID =" + IID);
if (ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode child = new TreeNode();
child.Text = dr["MenuName"].ToString().Trim();
string temp = dr["MenuID"].ToString();
child.Collapse();
parent.ChildNodes.Add(child);
FillChild(child, temp);
}
return 0;
}
else
{
return 0;
}
}
If you want something else state it clearly..e.g "Recursive table"?!.. if you mean following structure
Parent 1
-- Sub-Parent
--Child
Parent 2
-- Child
Parent 3
Parent 4
-- Child 1
-- Child 2
the above code by Moinul Islam is for you.. If not then explain "Recursive table"