我想在LinqToSql中使用asp Treeview控件

时间:2014-02-10 12:02:28

标签: c# asp.net visual-studio-2010 treeview

我试图了解如何实现树视图控件 - 它看起来非常复杂。但是,Treeview控件更合适。

我有一个包含字段ID和ParentLevelID的SQL表。

我在代码中添加了一个基本的Treeview控件:

        <asp:TreeView ID="tvLevels" runat="server">
        </asp:TreeView>

我想使用LinqToSQL填充此表。目前,我正在显示与Gridview相同的数据:

    protected void SetupLevelsPanel()
    {
        // display levels according to current parentId
        _svsCentralDataContext = new SVSCentralDataContext();
        object levels;
        if (_intParentLevelId == 0)
        {
            levels = (from sl in _svsCentralDataContext.SVSSurvey_Levels
            where sl.ParentLevelID == null && sl.SurveyID == _intSurveyId
            select new
            {
                sl.ID,
                sl.SurveyID,
                sl.UserCode,
                sl.ExternalRef,
                sl.Description,
                sl.ParentLevelID,
                sl.LevelSequence,
                sl.Active
            });
            backUpButton.Visible = false;
        }
        else
        {
            levels = (from sl in _svsCentralDataContext.SVSSurvey_Levels
            where sl.ParentLevelID == _intParentLevelId && sl.SurveyID == _intSurveyId
            select new
            {
                sl.ID,
                sl.SurveyID,
                sl.UserCode,
                sl.ExternalRef,
                sl.Description,
                sl.ParentLevelID,
                sl.LevelSequence,
                sl.Active
            });
        }


        grdLevels.DataSource = levels;
        grdLevels.DataBind();
        GrdLevelButtons();
    }

如何将此信息转换为使用Treeview控件?

1 个答案:

答案 0 :(得分:0)

这是我的解决方案。 在我的代码页面后面:

    private void BuildTree()
    {
        tvLevels .Nodes.Clear();
        _svsCentralDataContext = new SVSCentralDataContext();
        List<DataAccessLayer.Level> items = DataAccessLayer.Levels.GetLevels(_intSurveyId).ToList();

        List<DataAccessLayer.Level> rootItems = items.FindAll(p => p.ParentLevelId == null);

        foreach (DataAccessLayer.Level item in rootItems)
        {
            var tvi = new TreeNode(item.Description, item.Id.ToString(CultureInfo.InvariantCulture) );
            BuildChildNodes(tvi, items, item.Id);
            tvLevels.Nodes.Add(tvi);
        }
    }

    private void BuildChildNodes(TreeNode parentNode, List<DataAccessLayer.Level> items, int parentId)
    {
        List<DataAccessLayer.Level> children = items.FindAll(p => p.ParentLevelId == parentId).ToList();
        foreach (DataAccessLayer.Level item in children)
        {
            var tvi = new TreeNode(item.Description, item.Id.ToString(CultureInfo.InvariantCulture));
            parentNode.ChildNodes.Add(tvi);
            BuildChildNodes(tvi, items, item.Id);
        }
    }

Class Levels.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using SVSVoidSurveyDesigner.Database;

   namespace SVSVoidSurveyDesigner.DataAccessLayer
   {
   public class Levels
   {
    public static IEnumerable<Level> GetLevels(int intSurveyId)
    {
        var dataContext = new SVSCentralDataContext();


        var levels = (from l in dataContext.SVSSurvey_Levels where l.SurveyID == intSurveyId
                             select new Level
                             {
                                 Id = l.ID,
                                 SurveyId = l.SurveyID,
                                 UserCode = l.UserCode ,
                                 ExternalRef = l.ExternalRef ,
                                 Description = l.Description ,
                                 ParentLevelId = (l.ParentLevelID),
                                 LevelSequence = ( l.LevelSequence ),

                                 Active = Convert .ToBoolean( l.Active )
                             });

        return levels;
    }
}

}

Class Level.cs

    namespace SVSVoidSurveyDesigner.DataAccessLayer
    {
        public class Level
        {
        public int Id { get; set; }
        public int SurveyId { get; set; }
        public string UserCode { get; set; }
        public string ExternalRef { get; set; }
        public string Description { get; set; }
        public int? ParentLevelId { get; set; }
        public int? LevelSequence { get; set; }
        public bool Active { get; set; }
        }
    }