Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying create a TOC(Table Of Content) using a tree view with the parent/child relationship.

For example, my datatable is like

ID Parent_Id Name
1 0 AA
2 1 BB
3 1 CC
4 3 3rd Child Of CC
5 0 DD
6 5 EE
7 5 FF


I want show the tree node as

AA
------BB
------CC
*****------3rd Child Of CC
DD
------EE
------FF

Please help me sir
Posted
Updated 24-Apr-13 1:20am
v6
Comments
OriginalGriff 23-Apr-13 9:30am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.

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

C#
DataTable dt= new DataTable();
              dt= GetMenuData();// here inside get GetMenuData method you can write query like this - select * from yourtablename where parent_id=0
              PopulateParentNodes(dt);


2- Now you can write a PopulateParentNodes method for binding all parent nodes

C#
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-
C#
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);
           }
       }
 
Share this answer
 
Comments
_Raj 24-Apr-13 5:46am    
nice
kanamala subin 24-Apr-13 7:21am    
dis code is not working...it binding all child pages with every parent nodes
By using a recursion Method I completed this...

First suppose you have the data inside a datatable
Then use this code


void PopulateMenu()
{
dtTocData = new DataTable();
dtTocData = MacroInfo.dtToc;

if (dtTocData!=null && dtTocData.Rows.Count > 0)
{
DataTable dtParents = GetTOCByParent(0);
foreach (DataRow drTOC in dtParents.Rows)
{
AddSubMenu(drTOC, trvToc.Nodes);
}
}
}
private void AddSubMenu(DataRow drTOC, TreeNodeCollection tncTOC)
{
TreeNode oTOCNode;
DataTable dtTOCs = GetTOCByParent(Convert.ToInt32(drTOC["TocId"]));
oTOCNode = new TreeNode(Convert.ToString(drTOC["Name"]),Convert.ToString(drTOC["Name"]));
//oTOCNode.Selectable = false;
tncTOC.Add(oTOCNode);
foreach (DataRow drChildRow in dtTOCs.Rows)
{
AddSubMenu(drChildRow, oTOCNode.ChildNodes);
}
}

private DataTable GetTOCByParent(int TocId)
{
DataTable dtTOCs = new DataTable();
DataRow[] drTOC = null;
drTOC = dtTocData.Select("ParentId='" + TocId + "'");
if (drTOC.Length > 0)
{
dtTOCs = drTOC.CopyToDataTable();
}
return dtTOCs;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900