C# 按状态加载父/子问题的Treeview
我有下面的表结构,它可能不是一个正确的结构,但不幸的是,这就是我得到的C# 按状态加载父/子问题的Treeview,c#,asp.net,sql,treeview,C#,Asp.net,Sql,Treeview,我有下面的表结构,它可能不是一个正确的结构,但不幸的是,这就是我得到的 id | Name | Parent | Status 1 First 0 Active 2 Child 1 Active 3 2Child 2 Inactive 逻辑: 按父级加载根=0,状态为 按父ID和根之后每个级别的状态填充加载子级 我的问题是,如果状态为“非活动”,我想查看哪些选项是非活动的,我不能,因为前两个选项是活动的。我
id | Name | Parent | Status
1 First 0 Active
2 Child 1 Active
3 2Child 2 Inactive
逻辑:
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);
}
}
}
好吧,如果我是你,我会把整个表加载到内存中,然后用C#计算出你的树视图。这似乎比在SQL中尝试许多选项要容易得多。好吧,如果我是你,我只需将整个表加载到内存中,并将其加载到一个简单的轻型DTO类集合中,然后用C#计算出树视图。这似乎比在SQL中尝试许多选项要容易得多。下面是我们如何处理的 假设您有一个名为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>
{
}
最后,将记录添加到树中的递归方法:
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);
}
}
}
//
///将所有记录添加到指定节点集合的递归方法
///
///
///
私有void addNodeStoreTree(MyRecordCollection cRecords、TreeNodeCollection cNodes)
{
foreach(我的记录或记录在cRecords.Values中)
{
TreeNode oNode=新的TreeNode();
oNode.Text=oRecord.Name;
oNode.Tag=oRecord;
添加(oNode);
//现在添加节点的子节点(如果有)
if(oRecord.Children.Count!=0)
{
addNodeStoreTree(oRecord.Children,oNode.Nodes);
}
}
}
以下是我们如何处理的
假设您有一个名为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>
{
}
最后,将记录添加到树中的递归方法:
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);
}
}
}
//
///将所有记录添加到指定节点集合的递归方法
///
///
///
私有void addNodeStoreTree(MyRecordCollection cRecords、TreeNodeCollection cNodes)
{
foreach(我的记录或记录在cRecords.Values中)
{
TreeNode oNode=新的TreeNode();
oNode.Text=oRecord.Name;
oNode.Tag=oRecord;
添加(oNode);
//现在添加节点的子节点(如果有)
if(oRecord.Children.Count!=0)
{
addNodeStoreTree(oRecord.Children,oNode.Nodes);
}
}
}
这是在查询MySQL数据库吗?这是在查询MySQL数据库吗?老实说,我很困惑,我不知道你在建议什么。这听起来是个不错的解决方案,但我想不出来。我将编辑我的问题,以显示我到目前为止已经尝试了什么。老实说,我很困惑,我不知道你在建议什么。这是一个很好的解决方案听起来是一个很好的解决方案,但我想不出来。我将编辑我的问题以展示我迄今为止所做的尝试。谢谢!!我有人在这里帮我,他提出了相同的解决方案略有不同,但几乎相同。很高兴有你这样的人和这个伟大的网站非常感谢。我有人在这里帮我,他提出了相同的解决方案略有不同,但几乎相同。很高兴有你这样的人和这个伟大的网站