0

I have a Entry Class:

   public class Entry
   {
      public int Key { get; set; }
      public string Name { get; set; }
   }

and a Group Class:

   public class Group
   {
      public int Key { get; set; }
      public string Name { get; set; }

      public List<Group> SubGroups { get; set; }
      public List<Entry> Entries { get; set; }
      public List<object> Items
      {
         get
         {
            List<object> childNodes = new List<object>();
            foreach (var group in this.SubGroups)
               childNodes.Add(group);
            foreach (var entry in this.Entries)
               childNodes.Add(entry);

            return childNodes;
         }
      }
   }

I can Manually add data to treeView Like This:

public class TestData
   {

      public List<Group> Groups = new List<Group>();

      public void Load()
      {


         Group grp1 = new Group() { Key = 1, Name = "Group 1", SubGroups = new List<Group>(), Entries = new List<Entry>() };
         Group grp2 = new Group() { Key = 2, Name = "Group 2", SubGroups = new List<Group>(), Entries = new List<Entry>() };
         Group grp3 = new Group() { Key = 3, Name = "Group 3", SubGroups = new List<Group>(), Entries = new List<Entry>() };
         Group grp4 = new Group() { Key = 4, Name = "Group 4", SubGroups = new List<Group>(), Entries = new List<Entry>() };

         //grp1
         grp1.Entries.Add(new Entry() { Key = 1, Name = "Entry number 1" });
         grp1.Entries.Add(new Entry() { Key = 2, Name = "Entry number 2" });
         grp1.Entries.Add(new Entry() { Key = 3, Name = "Entry number 3" });

         //grp2
         grp2.Entries.Add(new Entry() { Key = 4, Name = "Entry number 4" });
         grp2.Entries.Add(new Entry() { Key = 5, Name = "Entry number 5" });
         grp2.Entries.Add(new Entry() { Key = 6, Name = "Entry number 6" });

         //grp3
         grp3.Entries.Add(new Entry() { Key = 7, Name = "Entry number 7" });
         grp3.Entries.Add(new Entry() { Key = 8, Name = "Entry number 8" });
         grp3.Entries.Add(new Entry() { Key = 9, Name = "Entry number 9" });

         //grp4
         grp4.Entries.Add(new Entry() { Key = 10, Name = "Entry number 10" });
         grp4.Entries.Add(new Entry() { Key = 11, Name = "Entry number 11" });
         grp4.Entries.Add(new Entry() { Key = 12, Name = "Entry number 12" });

         Groups.Add(grp1);
         Groups.Add(grp2);
         Groups.Add(grp3);
         Groups.Add(grp4);

         grp1.SubGroups.Add(grp2);
         grp2.SubGroups.Add(grp3);

      }

and its the Load Data Class

      private void LoadView()
      {
         TestData data = new TestData();
         data.Load();
         GroupView.ItemsSource = data.Groups;
      }

But, I wants to fill the treeView from database like this one:

ID    groupFlag     fatherID       Name
1        1            Null        Group1
2        1             1          Group1.1
3        0             2          Entry1
4        0             2          Entry2
5        1            Null        Group2
6        0             5          Entry3

any help would be appreciated...

nadaram
  • 101
  • 2
  • 15
  • I suppose your looking for recurse. Your `Group` is DB class? What kind of structure do you want in the end? – teo van kot Dec 24 '15 at 07:38
  • If you have a database with Group and Entry tables with some data, you will just need an sql command for that. Then you can make an sql connection and fill your treeview. – Orkun Bekar Dec 24 '15 at 08:01
  • Both Entry and Group, reads from DB, the table have a relation with itself, and FatherID points to Father group... – nadaram Dec 24 '15 at 09:02
  • Like this : https://i-msdn.sec.s-msft.com/dynimg/IC97405.jpeg – nadaram Dec 24 '15 at 17:28
  • Is your question "How do I convert a flat list to a hierarchy", along the lines of [this question](https://stackoverflow.com/questions/31363611/how-can-i-convert-a-list-of-items-with-parentid-into-a-tree) or [this question](https://stackoverflow.com/questions/9409021/), or is your question "How do I bind a WPF treeview to a hierarchy", along the lines of [this question](https://stackoverflow.com/questions/23812357/)? – dbc Dec 25 '15 at 16:05

1 Answers1

0

from what i understood from the question you have entries and groups mixed in a single table , which also includes nested groups , in order to put that data into the format you require (classes you mentioned in the question) i have wrote the following code which uses a temporary dictionary and does it in two steps:

Given the following input :

public class FlatItem {
    public int ID;
    public int groupFlag;
    public int? fatherID;
    public string Name;
}

var data = new List<FlatItem>();

data.Add(new FlatItem() { ID = 1, groupFlag = 1, fatherID = null, Name = "Group 1" });
data.Add(new FlatItem() { ID = 2, groupFlag = 1, fatherID = 1, Name = "Group 1.1" });
data.Add(new FlatItem() { ID = 3, groupFlag = 0, fatherID = 2, Name = "Entry 1" });
data.Add(new FlatItem() { ID = 4, groupFlag = 0, fatherID = 2, Name = "Entry 2" });
data.Add(new FlatItem() { ID = 5, groupFlag = 1, fatherID = null, Name = "Group 2" });
data.Add(new FlatItem() { ID = 6, groupFlag = 0, fatherID = 5, Name = "Entry 3" });

to convert it:

//temporary dictionary used only while converting
var Groups = new Dictionary<int,Group>();
//final variable where the result will be in
var hierarchy = new List<Group>();

//creating groups
foreach (var item in data.Where(x=> x.groupFlag==1)) {
    var grp = new Group() {
        Key = item.ID,
        Name = item.Name,
        Entries = new List<Entry>(),
        SubGroups = new List<Group>()
    };
    //store reference for later use
    Groups[item.ID] = grp;

    if (item.fatherID == null) {
        //store it in main list
        hierarchy.Add(grp);
    } else {
        //store it in its parent
        Groups[(int)item.fatherID].SubGroups.Add(grp);
    }
}

//fill entries
foreach(var item in data.Where(x=> x.groupFlag == 0)) {
    Groups[(int)item.fatherID].Entries.Add(new Entry() {
        Key = item.ID,
        Name = item.Name
    });
}

now the variable hierarchy should contain what you want, and you can garbage collect Groups which is no longer needed.

Bor691
  • 606
  • 1
  • 11
  • 21