如何在C#中构建嵌套的treeview?

时间:2016-11-30 06:06:32

标签: c# linq treeview

我正在使用dapper,asp.net web api并希望在treeview中显示数据。为此,我有2个模型,目前看起来如下。 数据库表映射:

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

视图映射(此模型将被视图使用)

public class Zones
{
    public string Id { get; set; }
    public string Name { get; set; }
    public List<Zones> Childrens { get; set; }
}

Dapper查询:

var zones = await
            con.QueryAsync<Zones>(
            sql: AdvisorQueries.AllZones,
            commandType: CommandType.Text);

数据库表:

enter image description here

如何使用LINQ在树视图模型中转换它?

2 个答案:

答案 0 :(得分:0)

在Zones模型中创建方法

{'no': [0, 1], 'I': [0, 1], 'gotta': [2], 'it': [2, 3], 'rain': [2, 3], 
'shame': [1], 'have': [0, 1], 'You': [2], 'pride': [0], 'Make': [3], 'make': [2]}

然后将区域模型类重命名为 childzone

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

因为类名不应该等于生成treeNode。

您可以像这样使用相同的Zones类

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

最后你会得到一个这样的模型

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

    }

输出

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

}

答案 1 :(得分:0)

您不需要树视图。您可以使用DataTable,其中第1列是布尔值

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

    }
}

enter image description here