2 - node_Name Hi;i have one table in SQL Server and Load Data in tree View with this code (in Entity Framework with recursive function) :my table have three feild :1 - node_Index2 - node_ParentIndex2 - node_Nameprivate void LoadTreeView1(){ // db is my database and tb1 is my table Table1 tb1 = new Table1(); tv1.Nodes.Clear(); using (var db = new dataBase1Entities()) { foreach (var item in db.Table1.Where(C => C.node_ParentIndex == 0)) { TreeNode tnParent = new TreeNode(item.node_Name); tv1.Nodes.Add(tnParent); int value = (int)item.node_Index; tnParent.Expand(); FillChild1(tnParent, value); } }}public int FillChild1(TreeNode parent, int parentIndex){ Table1 tb1 = new Table1(); using (var db = new dataBase1Entities()) { if ((db.Table1.Count() > 0)) { foreach (var item in db.Table1.Where(C => C.node_ParentIndex == parentIndex)) { TreeNode child = new TreeNode(item.node_Name.Trim()); parent.Nodes.Add(child); int value = (int)item.node_Index; child.Collapse(); FillChild1(child, value); } return 0; } else { return 0; } }} 但此代码不起作用并创造无限循环,我不能结束它。 请帮帮我!but this Codes not work and create infinite Loop and i can not end it. please help me !推荐答案 做这样的事情。 将null传递给第一次使用的item参数。 我手工编写但我希望它可以工作 Hi, do something like this.pass null to item parameter for the first using.I wrote it by hand but I hope it will work private void FillTreeItems(IEnumerable<node> myTables, TreeViewItem item){ foreach (var table in myTables) { var newItem = new TreeViewItem() { //Some Code... }; if (item == null) MyTreeView.Items.Add(newItem); else item.Items.Add(newItem); if (!table.Nodes.Any()) continue; foreach (var node in table.Nodes) { FillTreeItems(table.Nodes, newItem); } }} 这篇关于使用递归函数在TreeView中加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-01 03:50