2010-07-06 90 views
0

我有一個表「Category」。它只是一個用戶表,每個用戶都用一個唯一的UserId標識,並有一個相應的ParentId(指向他們的老闆UserId)。如果它是頂級用戶,則ParentId設置爲0.是否有人可以幫助我找出在樹視圖中填充此列表的最佳方式?如何填充treeview?

+0

該網站的一個或一種形式? – 2010-07-06 07:42:04

回答

1

如果你指的WinForms樹狀我會做這樣的事情(錯誤檢查跳過):

private void FillTreeView(object sender, EventArgs e) 
{ 
    // create fake datatable 
    DataTable dt = new DataTable(); 
    dt.Columns.Add("UserId",typeof(int)); 
    dt.Columns.Add("Name",typeof(string)); 
    dt.Columns.Add("ParentId", typeof(int)); 


    dt.Rows.Add(new object[] { 3, "Level_1_A", 2 }); 
    dt.Rows.Add(new object[] { 4, "Level_1_B", 2 }); 
    dt.Rows.Add(new object[] { 2, "Level_0_A", 0 }); 
    dt.Rows.Add(new object[] { 5, "Level_2_A", 3 }); 
    dt.Rows.Add(new object[] { 6, "Level_2_B", 3 }); 
    dt.Rows.Add(new object[] { 7, "Level_0_B", 0 }); 
    dt.Rows.Add(new object[] { 8, "Level_1_C", 7 }); 

    // call recursive function 
    AddCurrentChild(0, dt, treeView1.Nodes); 
} 

private static void AddCurrentChild(int parentId, DataTable dt, TreeNodeCollection nodes) 
{ 
    var rows = dt.Select("ParentId = " + parentId); 
    foreach (var row in rows) 
    { 
     var userId = (int) row["UserId"]; 
     var name = row["Name"] as string; 

     var node = nodes.Add(userId.ToString(), name.ToString()); 
     node.Tag = row; // if you need to keep a row reference on the node 
     AddCurrentChild(userId, dt, node.Nodes); 
    } 
} 
0

我想你知道如何讀取數據庫中的數據,所以我跳過這一部分。

抽象版本 - 您可以閱讀任何來源的員工。

一些幫手實體(你可以沒有他們OFC - 它只是更容易/更好用這種方式):

/// <summary>The employee structure with explicit conversion to a TreeNode (you may also use implicit conversion, but i prefer explicit).</summary> 
public struct Employee 
{ 
    public int Id; 
    public string Name; 
    public int BossId; 

    public static explicit operator TreeNode(Employee e) { return new TreeNode(e.Name); } 
} 

public static class EmployeesExtension 
{ 
    /// <summary>More abstract and readable way to add an employee.</summary> 
    public static void Add(this Dictionary<int, List<Employee>> employees, int id, string name, int bossId) 
    { 
     if (!employees.ContainsKey(bossId)) employees[bossId] = new List<Employee>(); 

     employees[bossId].Add(new Employee() { Id = id, Name = name, BossId = bossId }); 
    } 
} 

用於填充一個TreeView的方法:

public static void PopulateTreeView(Dictionary<int, List<Employee>> employees, int bossId, TreeNodeCollection nodes) 
{ 
    if (!employees.ContainsKey(bossId)) return; 

    foreach (Employee e in employees[bossId]) 
    { 
     TreeNode tn = (TreeNode)e; 
     nodes.Add(tn); 
     PopulateTreeView(employees, e.Id, tn.Nodes); 
    } 
} 

如何在代碼中使用它:

Dictionary<int, List<Employee>> employees = new Dictionary<int, List<Employee>>(); 

/* Here you will do the actual reading from DB */ 
//    id,  name,  bossId 
employees.Add(666, "The Master  ", 0); 
employees.Add(123, "The Underling 1", 666); 
employees.Add(879, "The Underling 2", 666); 
employees.Add(001, "The Slave 1 ", 123); 

this.treeView1.BeginUpdate(); 
PopulateTreeView(employees, 0, this.treeView1.Nodes); 
this.treeView1.EndUpdate(); 

Usin g方法將消除GUI的「閃爍」。