Curtis Schreiner

Curtis Schreiner

  • NA
  • 10
  • 37.9k

C# Netsted List into TreeView

Jan 26 2015 12:13 AM
Hey there,

I was seeing if you might be able to explain how I can throw the second part of my relative nested list back to the second level of my TreeNode for Treeview.

Quite honestly I don't know how to specify whether this type of list would be classified as a Nested List, Sub List, etc. I looked up quite a few examples, but I wasn't able to find anything that correlates to the subject I'm working on. I've highlighted the second for loop that I'm struggling list out the data properly below.

I've trimmed down my code to make the explanation a little easier.

#SchoolManager.cs
public static List<Department> GetDepartments()
{
//Department Data
Department it = new Department { Id = "IT", Name = "Information Technologies" };
Department bus = new Department { Id = "BUS", Name = "Business Management" };
Department con = new Department { Id = "CONST", Name = "Construction" };

//Add the Department data to the Departments List
List<Department> departments = new List<Department>();
departments.Add(it);
departments.Add(bus);
departments.Add(con);

//Course Data
Course vb1 = new Course { Code = "COMP123", Name = "VB.NET Level 1", Department = it };
Course vb2 = new Course { Code = "COMP222", Name = "VB.NET Level 2", Department = it };

//Add the Course Data to the IT Courses List
it.Courses = new List<Course>();
it.Courses.Add(vb1);
it.Courses.Add(vb2);

Department.cs
public class Department
{
public string Id { get; set; }
public string Name { get; set; }
public List<Course> Courses { get; set; }
}

Form1.cs
//Current this will return the root level "Department" to the Tree View
private void PopulateDepartment()
{
var departments = SchoolManager.GetDepartments();

//Creating the root node "Departments for the Tree View
var rootNode = treeView1.Nodes.Add("School");
rootNode.Tag = departments;

//The Department is the root node, so this first foreach loop will list out the Courses assigned to the IT Department
TreeNode departmentNode = null;

foreach (var department in departments)
{
//Instantiate the department Node
departmentNode = new TreeNode(department.Name);
departmentNode.Tag = department;

//This will add all of the available Departments to the root Department Node
rootNode.Nodes.Add(departmentNode);

//declare a course node
TreeNode courseNode = null;

foreach (var course in departments)
{
courseNode = new TreeNode(course.Courses[0].Name);
courseNode.Tag = course;

departmentNode.Nodes.Add(courseNode);
}

}

Basically I'm just wanting to list the VB courses under the Department Node of "Information Technologies". At the moment it looks something like this.

School
- Information Technologies
VB.NET Level 1
Math (non relative)
Blueprints (non relative)

Where I'm wanting it look like the following below.

School
- Information Technologies
VB.NET Level 1
VB.NET Level 2



I set a breakpoint on the start of the root level of the list foreach (var department in departments)
, and I can see the Course data is nested into their relative departments, however I'm just wanting to find the proper way I can list out the nested Courses into the second foreach loop and assign it the second level of the treeview. Please let me know if my question makes sense, and if not, I can certainly elaborate or correct what I've explained so far.

Answers (2)