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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Ifour HirenPosted Nov 3, 2016, 5:33 AM
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
mkariti mkaritiPosted May 30, 2011, 1:25 PM
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;
}
Mahesh ChandPosted May 30, 2011, 11:50 AM
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.
mkariti mkaritiPosted May 30, 2011, 11:08 AM
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
Mahesh ChandPosted May 30, 2011, 10:56 AM
TreeView control in asp.net 3.5
Display Directory Structure Using TreeView Control in ASP.NET
Mahesh ChandPosted May 30, 2011, 10:51 AM
ASP.NET TreeView
mkariti mkaritiPosted May 30, 2011, 12:33 AM
Many thanks
Mahesh ChandPosted May 29, 2011, 7:45 PM