I have the following table structure it might not be a correct structure but unfortunatly that’s what I was given.
id | Name | Parent | Status
1 First 0 Active
2 Child 1 Active
3 2Child 2 Inactive
Logic:
-
Load Root by Parent = 0 and Status
-
OnPopulate load child by parent ID and status for every levels after root
my issue is if the status is “Inactive” and I want to see what options are inactive I can’t because the first 2 options are active. What I need is to be able to view in my treeview all the levels down to the option that is Inactive or Active.
I have tried the following sql statement
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
I have ran out of ideas in sql and coding to figure this out..hope someone could guide me in the right direction..thanks
Also tried this in code:
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);
}
}
}
Here is how we handle this.
Assume that you have a class called MyRecord to hold each row of data from the DB:
Then you have a collection type to hold these records indexed by their id:
Here is the code (retrieval from the DB not shown) to preprocess the records and then add them to the tree:
And finally, the recursive method for adding the records to the tree: