Hi there,
I need to create a tree view menu that will be populated via a db as well as to be able to add as many childs as we want.
Any Ideas on how to approch this?
many thanks
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Mahesh ChandRank #1 · 393.3K reputationPosted May 30, 2011, 7:50 AM EDT
http://obout.com/em/em_whatis.aspx
To determine what is parent, what is a child, your database has to support that. It has to store level of a Tree node. For example, level 0 will be the root level nodes, with an ID. First level of nodes will have level = 1 and a parent. So database table should have these columns in addition to what you already have.
ID Parent ID Level Description
1 0 0 Root Node
2 1 1 First Child
3 1 1 Second Child
4 2 2 First Grand Child
When you build your tree, first you will get all the nodes with Level 0 and create them as root level of children. Then you will build a recursive loop and find the first root level node, find all children with parent 0 and add them to that root node and so on.
Mahesh ChandRank #1 · 393.3K reputationPosted May 29, 2011, 3:45 PM EDT
mkariti mkariti12 reputationPosted May 29, 2011, 8:33 PM EDT
Many thanks
Mahesh ChandRank #1 · 393.3K reputationPosted May 30, 2011, 6:51 AM EDT
ASP.NET TreeView
Mahesh ChandRank #1 · 393.3K reputationPosted May 30, 2011, 6:56 AM EDT
TreeView control in asp.net 3.5
Display Directory Structure Using TreeView Control in ASP.NET
mkariti mkariti12 reputationPosted May 30, 2011, 7:08 AM EDT
Many thanks Mahesh Chand,
But I've already looked at these post and unfortunately wasn't that helpful.
I need to populate the tree view from the database using entity and I cant seems to get it right.
How do you determine which will be the child and which will be the parent?
The old post (i.e .net 2 ) speeks about node.ID which I cant seems to find.
Cheers
mk
mkariti mkariti12 reputationPosted May 30, 2011, 9:25 AM EDT
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.DataSource = mydb.Recursives;
DropDownList1.DataTextField = "Title";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
if (!IsPostBack)
{
var parentlist = mydb.Recursives.Where(p => p.ParentID == null).First();
int? parId = 0;
TreeNode firstParent = getTreeNode(parentlist, ref parId);
TreeView1.Nodes.Add(firstParent);
addnew(firstParent, parId);
}
}
private void addnew(TreeNode firstParent, int? parId)
{
var childlist = mydb.Recursives.Where(p => p.ParentID == parId);
foreach (Recursive item in childlist)
{
int? pId = 0;
TreeNode tmp = getTreeNode(item, ref pId);
firstParent.ChildNodes.Add(tmp);
addnew(tmp , pId);
}
}
private static TreeNode getTreeNode(Recursive parentlist, ref int? parid)
{
TreeNode firstParent = new TreeNode();
firstParent.Text = parentlist.Title;
firstParent.Value = "" + parentlist.id;
parid = parentlist.id;
return firstParent;
}
Ifour Hiren61 reputationPosted Nov 3, 2016, 1:33 AM EDT
List<yourclass> child = _context.DataTable.Include(x => x.ChildTable).ToList().Where(x => parentIcfStandardIds.Contains(x.Id)).ToList();
var Nodes = RecurseIcfRows(child);
TreeView1.Nodes.AddRange(Nodes);
private TreeNode[] RecurseIcfRows(List<yourclass> rows)
{
var nodeList = new List<TreeNode>();
//TreeNode node = null;
foreach (var dr in rows)
{
var parent = new TreeNode { Text = dr.Word + "(" + dr.Code + ")" };
long noteId = dr.Id;
parent.Name = noteId.ToString();
parent.ToolTipText = noteId.ToString();
if (nodeList.Find(FindNode) == null)
{
var childRows = dr.ChildStandards;
if (childRows.Count > 0)
{
// Recursively call this function for all childRowsl
TreeNode[] childNodes = RecurseIcfRows(childRows.ToList());
// Add all childnodes to this node.
parent.Nodes.AddRange(childNodes);
}
// Mark this noteID as dirty (already added).
//doneNotes.Add(noteID);
nodeList.Add(parent);
}
}
// Convert this List<TreeNode> to an array so it can be added to the parent node/TreeView.
TreeNode[] nodeArr = nodeList.ToArray();
return nodeArr;
}
This Work only when parentid and childid in same datatable
like
ID Parent ID Level Description
1 0 0 Root Node
2 1 1 First Child
3 1 1 Second Child
4 2 2 First Grand Child