Treeview与父/子问题按状态加载

时间:2011-12-13 17:02:35

标签: c# asp.net sql treeview

我有以下表结构,它可能不是一个正确的结构,但不幸的是,这就是我所得到的。

id |  Name  | Parent  | Status
 1    First     0       Active
 2    Child     1       Active
 3    2Child    2       Inactive

逻辑:

  1. 按父级加载Root = 0和状态

  2. OnPopulate按父级ID加载子级,并在root

    之后为每个级别加载状态

    我的问题是,如果状态为“非活动”,并且我想查看哪些选项处于非活动状态,我不能,因为前两个选项处于活动状态。我需要的是能够在我的树视图中查看所有级别,直到非活动或活动的选项。

  3. 我尝试过以下sql语句

    select distinct
            m.Id,
            m.Name,
            m.Parent,
            m.[Status]
    from mytable m
    where m.Parent = 3 and m.[Status] = 'I'
    union
    select 
            Id,
            Name,
            Parent,
            [Status]
    from mytable
    where ID in(select distinct
            o.ID
    from mytable o
    where o.ID = 3 and o.[Status] = 'I') and Parent = 3
    

    我已经用完sql和编码的想法来解决这个问题......希望有人可以指导我朝着正确的方向发展..谢谢

    还在代码中尝试了这个:

        protected void mytree_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            //this is just a class that loads the values from db
            MYList templist = new ListSyFamily();
            templist.LoadAll();//(ddlStatus.SelectedValue, Convert.ToInt32(e.Node.Value));
    
            foreach (temp temp in templist)
            {
                if (temp.Status == ddlStatus.SelectedValue && temp.Parent == Convert.ToInt32(e.Node.Value))
                {
                    TreeNode child = new TreeNode();
                    child.Text = temp.Description;
                    child.Value = temp.Id.ToString();
                    if (child.ChildNodes.Count == 0)
                        child.PopulateOnDemand = true;
    
                    child.ToolTip = "Ver sub-opciones";
                    //child.SelectAction = TreeNodeSelectAction.SelectExpand;
    
                    child.CollapseAll();
    
                    e.Node.ChildNodes.Add(child);
                }
            }
        }
    

2 个答案:

答案 0 :(得分:2)

以下是我们处理此问题的方法。

假设您有一个名为MyRecord的类来保存数据库中的每一行数据:

public class MyRecord
{
    public int Id {get; set; }
    public int ParentId {get; set; }

    public string Name { get; set; }
    public string Status { get; set; }

    // The children of this node
    public MyRecordCollection Children = new MyRecordCollection();
}

然后你有一个集合类型来保存这些由他们的id索引的记录:

public class MyRecordCollection : System.Collections.Generic.Dictionary<int, MyRecord>
{

}

这是代码(从DB中检索未显示)来预处理记录,然后将它们添加到树中:

        MyRecordCollection cAllRecords;
        MyRecordCollection cParentRecords = new MyRecordCollection();

        // This is a method that just loads the records
        cAllRecords = LoadAllRecords();

        // Cycle through each of the records
        foreach (MyRecord oRecord in cAllRecords.Values)
        {
            if (oRecord.Id == 0)
            {
                // If the record is a parent record, add it to the list of parents
                cParentRecords.Add(oRecord.Id, oRecord);
            }
            else
            {
                // Otherwise, add the current record to its parent's list of children
                cAllRecords[oRecord.ParentId].Children.Add(oRecord.Id, oRecord);
            }
        }

        AddNodesToTree(cParentRecords, this.treeView1.Nodes);

最后,将记录添加到树中的递归方法:

    /// <summary>
    /// A recursive method to add all of the records to the specified collection of nodes
    /// </summary>
    /// <param name="cRecords"></param>
    /// <param name="cNodes"></param>
    private void AddNodesToTree(MyRecordCollection cRecords, TreeNodeCollection cNodes)
    {
        foreach (MyRecord oRecord in cRecords.Values)
        {
            TreeNode oNode = new TreeNode();
            oNode.Text = oRecord.Name;
            oNode.Tag = oRecord;
            cNodes.Add(oNode);
            // Now add the node's children if any
            if (oRecord.Children.Count != 0)
            {
                AddNodesToTree(oRecord.Children, oNode.Nodes);
            }
        }

    }

答案 1 :(得分:1)

好吧,如果我是你,我只需将整个表加载到内存中,然后将一个简单的DTO类集合加载到C#中进行树视图。这似乎比在SQL中尝试很多选项容易得多。