locked
Construct a Tree Node with dapper RRS feed

  • Question

  • User1410914394 posted

    HI 

    Can any one help me how to create a Tree structure using dapper in asp.net webforms.

    Wednesday, July 19, 2017 5:45 PM

All replies

  • User347430248 posted

    Hi Design Patte...,

    below is an example in C#. you can try to have a look.

    class TreeNode : IEnumerable<TreeNode>
    {
        private readonly Dictionary<string, TreeNode> _children =
                                            new Dictionary<string, TreeNode>();
    
        public readonly string ID;
        public TreeNode Parent { get; private set; }
    
        public TreeNode(string id)
        {
            this.ID = id;
        }
    
        public TreeNode GetChild(string id)
        {
            return this._children[id];
        }
    
        public void Add(TreeNode item)
        {
            if (item.Parent != null)
            {
                item.Parent._children.Remove(item.ID);
            }
    
            item.Parent = this;
            this._children.Add(item.ID, item);
        }
    
        public IEnumerator<TreeNode> GetEnumerator()
        {
            return this._children.Values.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    
        public int Count
        {
            get { return this._children.Count; }
        }
    }
    var tree = new TreeNode("Root")
                   {
                       new TreeNode("Category 1")
                           {
                               new TreeNode("Item 1"),
                               new TreeNode("Item 2"),
                               new TreeNode("Item 3"),
                           },
                       new TreeNode("Category 2")
                           {
                               new TreeNode("Item 1"),
                               new TreeNode("Item 2"),
                               new TreeNode("Item 3"),
                               new TreeNode("Item 4"),
                           }
                   };

    Reference:

    Build a simple, high performance Tree Data Structure in c#

    Tree/node class

    Regards

    Deepak

    Thursday, July 20, 2017 5:37 AM
  • User-691209617 posted
    sing System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                dataGridView1.AllowUserToAddRows = false;
    
                List<Zones> zones = new List<Zones>() {
                    new Zones() { 
                        Id = "AllZone", Name = "AllZone", Childrens = new List<Zones>() {
                            new Zones() { 
                                Id = "SZ001", Name = "All Stores", Childrens = new List<Zones>() {
                                    new Zones() { Id = "1", Name = "Express", Childrens = null},
                                    new Zones() { Id = "2", Name = "National", Childrens = null},
                                    new Zones() { Id = "3", Name = "Metro", Childrens = null},
                                    new Zones() { Id = "4", Name = "Scotland National", Childrens = null},
                                    new Zones() { Id = "5", Name = "Scotland Express", Childrens = null},
                                    new Zones() { Id = "6", Name = "UK London Metro", Childrens = null}
                                }
                            }
                        }
                    }
                };
    
                DataTable dt = new DataTable();
                dt.Columns.Add("Enabled", typeof(Boolean));
                dt.Columns.Add("ID", typeof(string));
                dt.Columns.Add("Name", typeof(string));
                dt.Columns.Add("Parent", typeof(string));
                dt.Columns["Parent"].AllowDBNull = true;
    
                Zones.GetChildren(dt, zones, "NULL");
                dt = dt.AsEnumerable().OrderBy(x => x.Field<string>("ID")).CopyToDataTable();
                dataGridView1.DataSource = dt;
            }
        }
        public class Zones
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public List<Zones> Childrens { get; set; }
    
            public static void GetChildren(DataTable dt, List<Zones> children, string parent)
            {
                foreach (Zones child in children)
                {
                    dt.Rows.Add(new object[] { false, child.Id, child.Name, parent });
                    if (child.Childrens != null)
                    {
                        GetChildren(dt, child.Childrens, child.Id);
                    }
                }
            }
    
        }
    }

    https://i.stack.imgur.com/daWRx.jpg
    Friday, July 21, 2017 7:33 PM