treeview 索引必须位于该列表的界限内。
protected void InitTree() { var childDataView = dataContext.TestTree.Where(c => c.parentId == 0).OrderBy(c => c.id).Select(c => c); foreach (var tree in childDataView) { var treeNode = new TreeNode { Text = tree.name, Value = tree.id.ToString() }; AddChildTree(treeNode, treeNode.Value); TreeViewTest.Nodes.Add(treeNode); } } /// <summary> /// 获取子节点 /// </summary> /// <param name="treeNode"> /// treeNode /// </param> /// <param name="parentId"> /// The parent id. /// </param> protected void AddChildTree(TreeNode treeNode, string parentId) { var childDataView = dataContext.TestTree.Where(c => c.parentId == int.Parse(parentId)).OrderBy(c => c.id).Select(c => c); foreach (var tree in childDataView) { var menuNode = new TreeNode { Text = tree.name, Value = tree.id.ToString() }; treeNode.ChildNodes.Add(treeNode); // 递归出现索引必须位于该列表的界限内错误 AddChildTree(menuNode, menuNode.Value); } }//create table TestTree//(// id int identity(1,1) primary key,// name varchar(20) not null,// parentId int//)