views:

22

answers:

1

I have a simple table in my database called Department. In it there's a column called ParentDepartmentID that is a self referential foreign key ID. Here's an example of the data I have for testing purposes:

alt text

As you can see, Departments: Diagnostics and XRay are children to Office of Chancellor. And Laboratories is child to Diagnostics.

This is how I retrive an IQueryable<Department> collection to my application using Entity Framework:

private DocumentsEntities db = new DocumentsEntities();

public IQueryable<Department> FindAllDepartments()
{
   return db.Departments;
}

What format does the TreeView control require in order to parse and display the tree hierarchy correctly? Thank you.

+1  A: 

I'm afraid that the winforms treeview is not designed so that you can send in parent/child relationship as many, more modern tree views do. You'll have to parse it yourself, creating and adding TreeNode objects (see for example here). In practice, I wouldn't recommend using this, companies have better things to do than implementing this kind of stuff again and again. Rather, consider buying a control library, e.g. from DevExpress or Telerik.

steinar