You can take these steps to use tree view with the parent/child relationship
1-First bind parent data on load event of form or page
DataTable dt= new DataTable();
dt= GetMenuData();
PopulateParentNodes(dt);
2- Now you can write a PopulateParentNodes method for binding all parent nodes
private void PopulateParentNodes(DataTable dt)
{
foreach (DataRow row in dt.Rows)
{
TreeNode newNode = new TreeNode(row["Name"].ToString(),
row["Id"].ToString());
TreeView1.Nodes.Add(newNode);
newNode.PopulateOnDemand = true;
}
}
3- Next you can generate an event TreeView1_TreeNodePopulate so that you can populate child nodes on demand-
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
DataTable dt= new DataTable();
int id = Convert.ToInt32(e.Node.Value);
dt= objplace.PopulateChildNodes(id);
foreach (DataRow row in dt.Rows)
{
TreeNode newNode = new TreeNode(row["Name"].ToString(),
row["Id"].ToString());
e.Node.ChildNodes.Add(newNode);
}
}