如何在C#中构建嵌套的Treeview? [英] How to build nested treeview in C#?

查看:134
本文介绍了如何在C#中构建嵌套的Treeview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用dapper,asp.net网络api,并希望在treeview中显示数据.为此,我有2个当前如下所示的模型. 数据库表映射:

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 class Zones
{
    public string model_zone_id { get; set; }
    public string model_zone_name { get; set; }
    public string model_zone_parent_id { get; set; }
}

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

View mapping (this model will be consumed by view)

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

数据库表:

如何使用LINQ在treeview模型中转换它?

How do I convert this in treeview model using LINQ?

推荐答案

您不需要树形视图.您可以使用第一个列为布尔值的DataTable

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

    }
}

这篇关于如何在C#中构建嵌套的Treeview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆