2015-12-24 55 views
0

我有一個入門級:如何從數據庫中氮水平填補了分層樹形視圖

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

和組類:

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; 
     } 
     } 
    } 

我可以將數據手工添加到TreeView的是這樣的:

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); 

     } 

及其負載數據類別

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

不過,我想從數據庫填充的TreeView像這樣的:

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 

任何幫助,將不勝感激......

+0

我想你在尋找遞歸。你的Group是DB類?你最終想要什麼樣的結構? –

+0

如果你有一個帶有一些數據的Group和Entry表的數據庫,你只需要一個sql命令。然後你可以建立一個sql連接並填充你的treeview。 –

+0

Entry和Group都從DB讀取,表與它自己有關係,並且FatherID指向父組... – nadaram

回答

0
從我從你有名片和分組的問題了解

混合在一個表中,它也包含嵌套組,以便將數據轉換爲您需要的格式(您在問題中提到的類)我已經編寫了下面的代碼,它使用了一個臨時字典並分兩步執行:

鑑於以下輸入:

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" }); 

將其轉換:

//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 
    }); 
} 

現在變量hierarchy應該包含你想要什麼,你可以收集垃圾其中Groups不再需要。

+0

Thanx很多,該代碼工作正常... :) – nadaram