Click here to Skip to main content
15,904,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends

I have a table(Hesab) whit 3 fields (ID,Name,FatherID), and use EF.
and i want fill this table in treeview

the tree must be fill like this
A
-D
--F
--E
-H
B
-C
--G

I Created this class
 class Hesabs:Hesab
    {
    public Hesabs()
    {
        AllHesab = new ObservableCollection<hesab>();
    }
    public ObservableCollection<hesab> AllHesab { get; set; }
           }

</hesab></hesab>


and viewmodel
 public ObservableCollection<hesabs> AllHesab { get; set; }
    public ObservableCollection<hesabs> FirstHesab { get; set; }

    public ViewModelHesabs()
    { 
        CRMEntities crm=new CRMEntities();

        var ls = from h in crm.Hesab
                 where (h.FatherID == null)
                 select h;
        ObservableCollection<hesabs> hes = new ObservableCollection<hesabs>();
        foreach (Hesab hh in ls.ToList())
        {
            var ls2 = from h in crm.Hesab
                      where (h.FatherID == hh.ID)
                      select h;
            Hesabs hesab = new Hesabs();
            hesab.Name = hh.Name;
            hesab.ID = hh.ID;
            hesab.AllHesab = new ObservableCollection<hesab>(ls2.ToList());
            hes.Add(hesab);
        }
        FirstHesab = hes;
    }

</hesab></hesabs></hesabs></hesabs></hesabs>

and xaml
<treeview itemssource="{Binding FirstHesab}">
     <treeview.itemtemplate>
     <hierarchicaldatatemplate itemssource="{Binding AllHesab}" datatype="         {x:Type local:Hesabs}">
             <stackpanel orientation="Horizontal">
                 <textblock text="{Binding Name}" tag="{Binding ID}" />
             </stackpanel>
         </hierarchicaldatatemplate>
     </treeview.itemtemplate>
 </treeview>


but fill 2 level only.:(
Posted

Hi,

Is the FatherID in the record a link to the same table, i.e. do you have a self-referencing entity? I think this is the case from the code but want to be sure.

You're problem appears to be that you select each 'h' , then go through and fill each h's children - I think you will need to do this operation recursively, otherwise you will only get the first level of children.
 
Share this answer
 
Hi ,
Yes, i have a self-referencing entity.
i change the code to
foreach (Hesab hh in crm.Hesab.ToList())
 
Share this answer
 
Comments
hzawary 22-Oct-11 6:27am    
Mr! Please use "Have a Question or Comment?" to send your comment:)
This place is for solution!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900