Retrieving data to Tree View
I have table like this
TaskID------ParentID-------TaskName
1-----------Null-----------AllPro
2-----------1--------------Drink
3-----------2--------------Coco
4-----------2--------------Pepsi
5-----------1--------------Food
6-----------5--------------Macdo
I try to get it in to tree view, my problem is when I want to make relation between task id and parent id the resault is duplicating as you can see my code:
The relation is between two column in one table:
public IQueryable<tblTask> GetT开发者_运维百科askTree()
{
var MySubject = from publisher in ObjectContext.tblTasks
join subjects in ObjectContext.tblTasks
on publisher.ParentTaskID equals subjects.TaskID
select publisher;
return MySubject;
}
This result is like this:
Drink
--Pepsi
--Coco
Pepsi
Coco
Food
---Mac
Mac
And here is itemtemplate
Could you plz help me.
I don't think you should have a join in your LINQ; your table should be read only once and having all records you should know how to nest them into each other with the recursive key ParentID, basically every record has an Id, a label and knows to which other parent record it belongs, no reason to join.
in old windows forms programming I would probably write a small procedure to scan all records and create nodes accordingly. There are also some hierarchical properties of the TreeView in general, so that you can specify the parent node field or something similar and the TreeView makes the hierarchy for you but really I am not sure about this with the default windows forms TreeView, I used so many controls that can't remember which ones has which features without checking... (DevExpress, Telerik, Infragistics...).
精彩评论