Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive TreeView in ASP.NET

I have an object of type list from which I wish to use to populate a treeview in asp.net c#.

Each object item has:

id | Name | ParentId

so for example:

id | Name     | ParentId
-------------------------
1  | Alice    | 0
2  | Bob      | 1
3  | Charlie  | 1
4  | David    | 2

In the above example, the parent would be Alice having two children Bob and Charlie. David is the child of Bob.

I have had many problems trying to dynamically populate the treeview recursively in c# ASP.NET

Does any one have a simple solution?

Btw: you can use People.Id, People.Name and People.ParentId to access the members since it is an object belonging to list.

I can post you my code so far (many attempts made) but not sure how useful it will be.

like image 792
waqasahmed Avatar asked Apr 03 '10 21:04

waqasahmed


People also ask

What is TreeView in asp net?

The TreeView control is used to display hierarchical data, such as a table of contents or file directory, in a tree structure and supports the following features: Data binding that allows the nodes of the control to be bound to XML, tabular, or relational data.

What is TreeView in C#?

TreeView control in C# is used to display the items in hierarchical form. Each item in TreeView control is called a node. A node can have sub-nodes too; and each sub-node has it's own nodes, and so on. All the nodes in the TreeView control are displayed in an hierarchical form, for better readability and control.

Which is the main property of TreeView?

The key properties of the TreeView control are Nodes and SelectedNode. The Nodes property contains the list of top-level nodes in the tree view. The SelectedNode property sets the currently selected node. You can display icons next to the nodes.

How do I get all child nodes of TreeView in VB net?

Solution: In a NodeSelectedEvent event, the event argument (e) holds the object of the selected node – Node. The selected node object have property called Items, it holds a list of child node object of the selected node. By iterating the Item object, you can get the information of each child node.


2 Answers

This is a sample with Category entity that references itself. First we should prepare our data source:

public class Category
    {
        public int Id { get; set; }
        public string  Name { get; set; }
        public int? ParentId { get; set; }
        public virtual Category Parent { get; set; }
        public virtual ICollection<Category> Children { get; set; }
        public byte[] Image { get; set; }
    }

public class Product
    {
        public int Id { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
        public Category ProductCategory { get; set; }
        public int ProductCategoryId { get; set; }
        public byte[] Image { get; set; }
    }

public List<Category> GethierarchicalTree(int? parentId=null)
        {
            var allCats = new BaseRepository<Category>().GetAll();

            return allCats.Where(c => c.ParentId == parentId)
                            .Select(c => new Category()
                            {
                                Id = c.Id,
                                Name = c.Name,
                                ParentId = c.ParentId,
                                Children = GetChildren(allCats.ToList(), c.Id)
                            })
                            .ToList();
        }

        public List<Category> GetChildren(List<Category> cats, int parentId)
        {
            return cats.Where(c => c.ParentId == parentId)
                    .Select(c => new Category
                    {
                        Id = c.Id,
                        Name = c.Name,
                        ParentId = c.ParentId,
                        Children = GetChildren(cats, c.Id)
                    })
                    .ToList();
        }

Then in our code behind we have:

 protected void Page_Load(object sender, EventArgs e)
        {
            var hierarchicalData = new CategoryRepository().GethierarchicalTree();
            tv1.Nodes.Clear();
            var root = new TreeNode("0","Root");
            tv1.Nodes.Add(root);
            BindTreeRecursive(hierarchicalData, root);
        }

        private void BindTreeRecursive(List<Category> hierarchicalData, TreeNode node)
        {
            foreach (Category category in hierarchicalData)
            {
                if (category.Children.Any())
                {
                    var n = new TreeNode(category.Name, category.Id.ToString());
                    node.ChildNodes.Add(n);
                    BindTreeRecursive(category.Children.ToList(), n);
                }
                else
                {
                    var n = new TreeNode(category.Name, category.Id.ToString());
                    node.ChildNodes.Add(n);

                    if (new ProductRepository().Get(a => a.ProductCategoryId == category.Id).Any())
                    {
                        var catRelatedProducts = new ProductRepository().Get(a => a.ProductCategoryId == category.Id).ToList();

                        foreach (Product product in catRelatedProducts)
                        {
                            n.ChildNodes.Add(new TreeNode(product.Name,product.Id.ToString()));
                        }
                    }
                }
            }
        }
like image 197
Arash Masir Avatar answered Oct 20 '22 06:10

Arash Masir


I think this should get you started. I created a MyObject class to mimic your object .

public class MyObject
{
    public int Id;
    public int ParentId;
    public string Name;
}

Here is a method to recursivley add tree view nodes based on the list.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        List<MyObject> list = new List<MyObject>();
        list.Add(new MyObject(){Id=1, Name="Alice", ParentId=0});
        list.Add(new MyObject(){Id=2, Name="Bob", ParentId=1});
        list.Add(new MyObject(){Id=3, Name="Charlie", ParentId=1});
        list.Add(new MyObject(){Id=4, Name="David", ParentId=2});            

        BindTree(list, null);            
    }
}

private void BindTree(IEnumerable<MyObject> list, TreeNode parentNode)
{
    var nodes = list.Where(x => parentNode == null ? x.ParentId == 0 : x.ParentId == int.Parse(parentNode.Value));
    foreach (var node in nodes)
    {
        TreeNode newNode = new TreeNode(node.Name, node.Id.ToString());
        if (parentNode == null)
        {
            treeView1.Nodes.Add(newNode);
        }
        else
        {
            parentNode.ChildNodes.Add(newNode);
        }
        BindTree(list, newNode);
    }
}
like image 27
Matt Dearing Avatar answered Oct 20 '22 07:10

Matt Dearing