日期:2014-05-17  浏览次数:20527 次

treeview 索引必须位于该列表的界限内。
C# code


   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
//)



递归出现索引必须位于该列表的界限内错误,不知道错在哪里啊,谁帮我看看。表结构

------解决方案--------------------
good
------解决方案--------------------
学习下 楼主var childDataView = dataContext.TestTree.Where(c => c.parentId == 0).OrderBy(c => c.id).Select(c => c);
给解释下呗 我知道是绑定数据用的 但是为什么这么写呢???不是很明白