How to build nested treeview in C#?

后端 未结 2 956
一个人的身影
一个人的身影 2021-01-26 07:54

I am using dapper, asp.net web api and want to show data in treeview. For this, I have 2 models that currently looks like below. Database table mapping:

public c         


        
相关标签:
2条回答
  • 2021-01-26 08:30

    You don't need a treeview. You can use a DataTable with 1st column being a boolean

    using 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);
                    }
                }
            }
    
        }
    }
    

    0 讨论(0)
  • 2021-01-26 08:45

    Create a method in Zones model

    public void PrintPretty(string indent, bool last)
        {
            Console.Write(indent);
            if (last)
            {
                Console.Write("|-");
                indent += "  ";
            }
            else
            {
                Console.Write("|-");
                indent += "| ";
            }
            Console.WriteLine(Name);
    
            for (int i = 0; i < Children.Count; i++)
                Children[i].PrintPretty(indent, i == Children.Count - 1);
        }
    

    then rename the zones model class to childzone

    public class childzones
    {
        public string model_zone_id { get; set; }
        public string model_zone_name { get; set; }
        public string model_zone_parent_id { get; set; }
    }
    

    because class names should not be equal for generating treeNode.

    You can use the same Zones class like this

     public class Zones
        {
    
            public Zones()
            {
                Children = new List<Zones>();
            }
            public string Id { get; set; }
            public string Name { get; set; }
            public List<Zones> Children { get; set; }
    
            public void PrintPretty(string indent, bool last)
            {
                Console.Write(indent);
                if (last)
                {
                    Console.Write("|-");
                    indent += "  ";
                }
                else
                {
                    Console.Write("|-");
                    indent += "| ";
                }
                Console.WriteLine(Name);
    
                for (int i = 0; i < Children.Count; i++)
                    Children[i].PrintPretty(indent, i == Children.Count - 1);
            }
    
        }
    

    Finally you will get a Model like this

    static void Main(string[] args)
            {
    
                Zones node1 = new Zones()
                {
                    Name = "Root",
                    Id = "1",
                    Children = {
                        new Zones() {
                            Name = "BranchA",
                           Id = "1",
                            Children = {
                                new Zones() {
                                    Name = "Siblings1",
                                     Id = "1",
                                    Children = {
                                        new Zones() {
                                            Name = "subChild1",
                                             Id = "1",
                                            Children = {
                                            }
                                        },
                                        new Zones() {
                                            Name = "subchild2",
                                             Id = "1",
                                            Children= {
                                            }
                                        }
                                    }
    
                                },
                                new Zones() {
                                    Name = "Siblings2",
                                     Id = "1",
                                    Children = {
    
                                    }
                                }
                            }
                        },
                        new Zones() {
                            Name = "BranchB",
                             Id = "1",
                            Children = {
                                new Zones() {
                                    Name = "Sibilings1",
                                     Id = "1",
                                    Children = {
    
                                    }
                                }
                            }
                        }
                    }
                };
                node1.PrintPretty("", true);
    
    }
    

    Output

    |-Root
      |-BranchA
      | |-Siblings1
      | | |-subChild1
      | | |-subchild2
      | |-Siblings2
      |-BranchB
        |-Sibilings1
    
    0 讨论(0)
提交回复
热议问题