I have a table \"Category\".It is simply a table of users, where each are identified with a unique UserId and have a corresponding ParentId (pointing to their boss\' UserId). I
Just posted something similar on another post: Adding child nodes to tree view based on single database table with parent/child links
I guess you know how to read data from database so I skip that part.
An abstract version - you could read the employees from any source.
Some helper entities (you can do without them ofc - it is just easier/nicer to use this way):
/// <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 });
}
}
The method used to populate a 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);
}
}
How to use it in your code:
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();
Using the BeginUpdate
/ EndUpdate
methods will eliminate the "blinking" of your GUI.
If you mean WinForms treeview I would do something like this (error checking skipped):
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);
}
}