如何填充treeview?

时间:2010-07-06 06:56:51

标签: c# treeview

我有一个表“类别”。它只是一个用户表,其中每个用唯一的UserId标识并具有相应的ParentId(指向他们的老板'UserId)。如果它是顶级用户,则ParentId设置为0.有人可以帮我找出在树视图中填充此列表的最佳方法吗?

3 个答案:

答案 0 :(得分:2)

如果你的意思是WinForms treeview我会做这样的事情(错误检查跳过):

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

答案 1 :(得分:1)

我想你知道如何从数据库中读取数据,所以我跳过了那一部分。

抽象版 - 您可以从任何来源阅读员工。

一些辅助实体(你可以不使用它们 - 使用这种方式更容易/更好):

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

使用BeginUpdate / EndUpdate方法可以消除GUI的“闪烁”。

答案 2 :(得分:1)