Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.57/5 (6 votes)
hi ,

i have this table name is Job_Type_tab

id father Name
1 NULL A
2 NULL B
3 2 C
4 1 D
5 4 E
6 4 F
7 3 G
8 1 H

and i want fill this table in treeview

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


iwrite this code but this not good
private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var g = from h in Context.Job_Type_tab
                    where h.father == null
                    select h;
            List<Job_Type_tab> j = g.ToList<Job_Type_tab>();
            for (int i = 0; i < j.Count; i++ )
            {
                TreeViewItem treefather = new TreeViewItem();
                treefather.Header = j[i].Name;
                treebig.Items.Add(treefather);
                Int64 id = j[i].job_type_id;
                ScanChiled(id,treefather);
                
            }
        }

        public void ScanChiled(Int64 father,TreeViewItem treefather)
        {
            if (father == null)
                return;
            TreeViewItem treeChiled = new TreeViewItem();
            var c = from h in DLandS.Program.Context.Job_Type_tab
                    where h.father == father
                    select h;
            List<DLandS.Job_Type_tab> j = c.ToList<DLandS.Job_Type_tab>();
            for (int i = 0; i < j.Count; i++)
            {
                TreeViewItem tree = new TreeViewItem();
                treefather.Header = j[i].Name;
                treeChiled.Items.Add(tree);
                Int64 id = j[i].job_type_id;
                ScanChiled(id ,tree);
                
            }
            treefather.Items.Add(treeChiled);
        }



help me.........
Posted
Comments
Sergey Alexandrovich Kryukov 28-Jun-11 3:12am    
No idea: what's the problem?
--SA
Noze 28-Jun-11 5:45am    
t want code to fill this grid becase this code false

1 solution

This is much easier to achieve if you use the databinding features of WPF. If I was writing this, I'd create a class that represented the hierarchy of data that you want to display - this would be bound to from the XAML. I would write the class to look something like this:
C#
public class JobType
{
  public JobType()
  {
    JobTypes = new ObservableCollection<JobType>();
  }
  public ObservableCollection<JobType> JobTypes { get; set; }
  public string Name { get; set; }
}
Next, I'd add a reference to this class in my view model like this:
C#
public ObservableCollection<JobType> JobTypes { get; set; }
We are going to bind the treeview to this top level item, and use a HierarchicalDataTemplate to bind to the JobTypes entry in each instance of JobType.
C#
<TreeView ItemsSource="{Binding JobTypes}">
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding JobTypes}" DataType="{x:Type local:JobType}">
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Name}" />
      </StackPanel>
    </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>
</TreeView>
Make sure that you add a reference to your XAML to the namespace that contains the JobType class (I've called it local in the DataType markup).
 
Share this answer
 
Comments
Noze 28-Jun-11 5:44am    
hi ,
look i use entityFramwork

i use linq for give data from entity from table JobType

i want fill treeview from this table
in this code i display just
A
B

help me....
Pete O'Hanlon 28-Jun-11 5:53am    
So, you downvote the code and then you expect me to help. I've given you the model that you need to bind into and the XAML that you use. All you have to do is iterate over the hierarchy and add the data into the JobType model; that's the easy part.
Mark Salsbery 28-Jun-11 19:24pm    
...or, since you're already using LINQ to "for give data from entity from table JobType", shape the data into a hierarchical class as shown by Pete instead of shaping it into flat records.

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