积极答复者
求指教:LINQ如何动态绑定到TreeView?

问题
答案
-
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
全部回复
-
弄两个表(在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 -
弄两个表(在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
能否编程实现呢?我还是折腾不出来 -
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
-
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