How to build nested treeview in C#?

后端 未结 2 947
一个人的身影
一个人的身影 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: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();
            }
            public string Id { get; set; }
            public string Name { get; set; }
            public List 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
    

提交回复
热议问题