none
求指教:LINQ如何动态绑定到TreeView? RRS feed

  • 问题

  • 本人正在学习LINQ,突然有一个想法,能否使用LINQ绑定TreeView?我试了好几次都没有成功,哪位师父能否指点一下,数据库应当如何设置,如何绑定到TreeView?希望是非递归的,本人不喜欢递归,不好意思哈,小小要求。

    2013年11月13日 1:12

答案

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace CSharp
    {
        public partial class Form1 : Form
        {
            private class Node:TreeNode
            {
                public int ParentId { get; set; }
                public int Id { get; set; }
            }
    
            /// <summary>
            /// 声明一个TreeNode数组
            /// </summary>
            private List<Node> nodes = new List<Node>();
    
            public Form1()
            {
                InitializeComponent();
    
                //动态初始化
                nodes.Add(new Node { Text = "F", ParentId = 0, Id = 1 });
                nodes.Add(new Node { Text = "FM", ParentId = 1, Id = 2 });
                nodes.Add(new Node { Text = "FW", ParentId = 1, Id = 3 });
                nodes.Add(new Node { Text = "FM1", ParentId = 2, Id = 4 });
                nodes.Add(new Node { Text = "FM2", ParentId = 2, Id = 5 });
                nodes.Add(new Node { Text = "FW1", ParentId = 3, Id = 6 });
            }
    
            private void AutoAdd(Node node)
            {
                var result = from n in nodes
                             where n.ParentId == node.Id
                             select n;
    
                    foreach (var item in result)
                    {
                        node.Nodes.Add(item);
                        AutoAdd(item);
                    }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                AutoAdd(nodes[0]);
                treeView1.Nodes.Add(nodes[0]);
            }
        }
    }

    ASP.NET Questions
    Other Discussions
    FreeRice Donate
    Issues to report

    • 已标记为答案 蒙田 2013年11月16日 14:51
    2013年11月15日 2:28

全部回复

  • 弄两个表(在SQL Management Studio中的Diagram文件夹中)创建一对多关系,然后直接拖拽到LINQ TO SQL的DBML文件中(自动生成一对多)。然后循环遍历“一”的内容,动态添加到TreeView作为根节点,然后内部foreach遍历“多”的表,添加到该根节点的Children节点上作为其子节点。

    For Account Validation, please follow "Verify Account+Number" at http://social.msdn.microsoft.com/Forums/en-us/home?forum=reportabug

    For ASP.NET Question, please ask at http://forums.asp.net

    For other questions, you can find a specific forum and then ask at http://stackexchange.com/sites

    Click and Donate at http://www.freerice.com

    2013年11月13日 5:09
  • 弄两个表(在SQL Management Studio中的Diagram文件夹中)创建一对多关系,然后直接拖拽到LINQ TO SQL的DBML文件中(自动生成一对多)。然后循环遍历“一”的内容,动态添加到TreeView作为根节点,然后内部foreach遍历“多”的表,添加到该根节点的Children节点上作为其子节点。

    For Account Validation, please follow "Verify Account+Number" at http://social.msdn.microsoft.com/Forums/en-us/home?forum=reportabug

    For ASP.NET Question, please ask at http://forums.asp.net

    For other questions, you can find a specific forum and then ask at http://stackexchange.com/sites

    Click and Donate at http://www.freerice.com


    能否编程实现呢?我还是折腾不出来
    2013年11月15日 0:44
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace CSharp
    {
        public partial class Form1 : Form
        {
            private class Node:TreeNode
            {
                public int ParentId { get; set; }
                public int Id { get; set; }
            }
    
            /// <summary>
            /// 声明一个TreeNode数组
            /// </summary>
            private List<Node> nodes = new List<Node>();
    
            public Form1()
            {
                InitializeComponent();
    
                //动态初始化
                nodes.Add(new Node { Text = "F", ParentId = 0, Id = 1 });
                nodes.Add(new Node { Text = "FM", ParentId = 1, Id = 2 });
                nodes.Add(new Node { Text = "FW", ParentId = 1, Id = 3 });
                nodes.Add(new Node { Text = "FM1", ParentId = 2, Id = 4 });
                nodes.Add(new Node { Text = "FM2", ParentId = 2, Id = 5 });
                nodes.Add(new Node { Text = "FW1", ParentId = 3, Id = 6 });
            }
    
            private void AutoAdd(Node node)
            {
                var result = from n in nodes
                             where n.ParentId == node.Id
                             select n;
    
                    foreach (var item in result)
                    {
                        node.Nodes.Add(item);
                        AutoAdd(item);
                    }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                AutoAdd(nodes[0]);
                treeView1.Nodes.Add(nodes[0]);
            }
        }
    }

    ASP.NET Questions
    Other Discussions
    FreeRice Donate
    Issues to report

    • 已标记为答案 蒙田 2013年11月16日 14:51
    2013年11月15日 2:28
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace CSharp
    {
        public partial class Form1 : Form
        {
            private class Node:TreeNode
            {
                public int ParentId { get; set; }
                public int Id { get; set; }
            }
    
            /// <summary>
            /// 声明一个TreeNode数组
            /// </summary>
            private List<Node> nodes = new List<Node>();
    
            public Form1()
            {
                InitializeComponent();
    
                //动态初始化
                nodes.Add(new Node { Text = "F", ParentId = 0, Id = 1 });
                nodes.Add(new Node { Text = "FM", ParentId = 1, Id = 2 });
                nodes.Add(new Node { Text = "FW", ParentId = 1, Id = 3 });
                nodes.Add(new Node { Text = "FM1", ParentId = 2, Id = 4 });
                nodes.Add(new Node { Text = "FM2", ParentId = 2, Id = 5 });
                nodes.Add(new Node { Text = "FW1", ParentId = 3, Id = 6 });
            }
    
            private void AutoAdd(Node node)
            {
                var result = from n in nodes
                             where n.ParentId == node.Id
                             select n;
    
                    foreach (var item in result)
                    {
                        node.Nodes.Add(item);
                        AutoAdd(item);
                    }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                AutoAdd(nodes[0]);
                treeView1.Nodes.Add(nodes[0]);
            }
        }
    }

    ASP.NET Questions
    Other Discussions
    FreeRice Donate
    Issues to report

    感谢您的回复,虽然是数组,但是也受到启发了。
    2013年11月16日 14:52