【问题标题】:ASP.NET Tree View out of Sql BindingSql Binding 中的 ASP.NET 树视图
【发布时间】:2020-08-31 14:10:09
【问题描述】:

我有一个类别表,其中包含我的数据库中的树视图列,我想将其显示为树视图。

我的 TreeView 列如下所示。 (希望你能理解)

0001.
0001.0001.
0001.0002.
0001.0002.0001.
0001.0002.0002.
0001.0003.
0002.
...
...
...

这可以显示在 asp:TreeView 中吗?或者我可以用什么其他方式来显示这个?

数据库已经存在我只是在设计网站,在数据库中做不了太多事情。

编辑:

我尝试了第一个解决方案,现在我得到了这个。 Image

但我不想显示数字,只显示名称和数字,就像我点击它时不可见的值一样。

【问题讨论】:

    标签: c# sql asp.net treeview


    【解决方案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 WindowsFormsApplication64
    {
        public partial class Form1 : Form
        {
            DataTable dt = null;
            public Form1()
            {
                InitializeComponent();
    
                dt = new DataTable();
                dt.Columns.Add("Index", typeof(string));
                dt.Columns.Add("Value", typeof(string));
    
                dt.Rows.Add(new object[] { "0001.", "aaaa"});
                dt.Rows.Add(new object[] { "0001.0001.", "bbbb"});
                dt.Rows.Add(new object[] { "0001.0002.", "cccc"});
                dt.Rows.Add(new object[] { "0001.0002.0001.", "dddd"});
                dt.Rows.Add(new object[] { "0001.0002.0002.", "eeee"});
                dt.Rows.Add(new object[] { "0001.0003.", "ffff"});
                dt.Rows.Add(new object[] { "0002.", "gggg" });
    
                new Index(dt);
    
                MakeTreeRecursive(0, Index.indexes, null);
                treeView1.ExpandAll();
    
            }
            public void MakeTreeRecursive(int level, List<Index> rows, TreeNode parent)
            {
                TreeNode child = null;
    
                var groups = rows
                    .OrderBy(x => x.paragraphs[level])
                    .GroupBy(x => x.paragraphs[level]);
    
                foreach (var group in groups)
                {
                    child = new TreeNode(group.Key);
                    if (parent == null)
                    {
                        treeView1.Nodes.Add(child);
                    }
                    else
                    {
                        parent.Nodes.Add(child);
                    }
    
                    foreach (Index node in group.Where(x => x.paragraphs.Count == level + 1))
                    {
                        child.Nodes.Add(node.row.Field<string>("Value"));
                    }
                    List<Index> descendants = group.Where(x => x.paragraphs.Count > level + 1).ToList();
                    if (descendants.Count > 0)
                    {
                        MakeTreeRecursive(level + 1, descendants, child);
                    }
    
                }
            }
        }
        public class Index
        {
            public static List<Index> indexes { get; set; }
            public List<string> paragraphs { get; set; }
            public DataRow row { get; set; }
    
            public Index() { }
            public Index(DataTable dt)
            {
                Index.indexes =  dt.AsEnumerable()
                    .Select(x => new Index() {
                        row = x,
                        paragraphs = x.Field<string>("Index")
                        .Split(new char[] {'.'}, StringSplitOptions.RemoveEmptyEntries)
                        .Select(y=> y).ToList()
                    }).ToList();
    
            }
        }
    }
    

    如果你只想要文本,这里是解决方案

    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
        {
            DataTable dt = null;
            public Form1()
            {
                InitializeComponent();
    
                dt = new DataTable();
                dt.Columns.Add("Index", typeof(string));
                dt.Columns.Add("Value", typeof(string));
    
                dt.Rows.Add(new object[] { "0001.", "aaaa" });
                dt.Rows.Add(new object[] { "0001.0001.", "bbbb" });
                dt.Rows.Add(new object[] { "0001.0002.", "cccc" });
                dt.Rows.Add(new object[] { "0001.0002.0001.", "dddd" });
                dt.Rows.Add(new object[] { "0001.0002.0002.", "eeee" });
                dt.Rows.Add(new object[] { "0001.0003.", "ffff" });
                dt.Rows.Add(new object[] { "0002.", "gggg" });
    
                new Index(dt);
    
                MakeTreeRecursive(0, Index.indexes, null);
                treeView1.ExpandAll();
    
            }
            public void MakeTreeRecursive(int level, List<Index> rows, TreeNode parent)
            {
                TreeNode child = null;
    
                var groups = rows
                    .OrderBy(x => x.paragraphs[level])
                    .GroupBy(x => x.paragraphs[level]);
    
                foreach (var group in groups)
                {
                    Index root = group.OrderBy(x => x.paragraphs.Count).First();
                    child = new TreeNode(root.row.Field<string>("Value"));
    
                    List<Index> descendants = group.Where(x => x.paragraphs.Count > level + 1).ToList();
                    if (descendants.Count()  > 0)
                    {
                        MakeTreeRecursive(level + 1, descendants, child);
                    }
                    if (parent == null)
                    {
                        treeView1.Nodes.Add(child);
                    }
                    else
                    {
                        parent.Nodes.Add(child);
                    }
                }
            }
        }
        public class Index
        {
            public static List<Index> indexes { get; set; }
            public List<string> paragraphs { get; set; }
            public DataRow row { get; set; }
    
            public Index() { }
            public Index(DataTable dt)
            {
                Index.indexes = dt.AsEnumerable()
                    .Select(x => new Index()
                    {
                        row = x,
                        paragraphs = x.Field<string>("Index")
                        .Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries)
                        .Select(y => y).ToList()
                    }).ToList();
    
            }
        }
    }
    

    【讨论】:

    • 谢谢,我试过你的解决方案。唯一不起作用的是 parent/child.Nodes。我将其更改为 parent/child.ChildNodes 并给出了结果。但我不想显示数字,只是名称和数字就像我点击它时不可见的值一样。 -> 在我的问题中查看图片。
    • 有没有办法让它延迟加载?因为数据库的结构非常糟糕,所有类别加起来大约有 14'000 行,尽管所有项目只有大约 5'000 行。现在,如果它每次重新加载都加载 14'000,则需要的时间太长了。
    • 要消除“值”,只需删除将“值”添加到树中的 for 循环。当表单应用程序运行缓慢时,我通常将代码放在后台工作程序中,这样它就不会锁定表单。
    • 我不想删除文本,我想删除数字
    • 所以你想要aaaa,bbbb
    猜你喜欢
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 2010-12-09
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多