Sorry if I'm a bad teacher or am not explaining things correctly but I still think you are looking for something "recursive".
so like this would be a real dirty example of what that would be for you...
NOTE: I'm putting "..." in places that I don't want to type out some details
public void LoadUpNodes(TreeNodeCollection collection, string parentId)
{
// Get a list of children from the database...
query = "SELECT ... WHERE parent_id=" + parentId + ";";
connection = new SqlConnection(...
command = connection.CreateCommand();
command.CommandText = query;
reader = command.ExecuteReader();
// Go through each child that belongs to the specified parentId...
while (reader.Read())
{
// add this child to the given treenode collection
TreeNode newChild = collection.Add(reader["name"], reader["somethingelse"]);
// Now here is where recursion comes in:
// You can call this same function to load all of the children of
// this child node. By doing so, then in turn this function will
// be called again to load the children of each child of this
// child node. And then again for each child of each child of
// each child of each child and so on...
LoadUpNodes(newChild.Nodes, reader["id"]);
// Now, once the entire hierarchy for this child has been loaded
// this will continue loading the top most children and all of
// their hierarchies, right here where you left off...
// Note that each child "TreeNode" has a ".Nodes" property where
// its children are stored. By passing that .Nodes property to
// this function along with the id of this child, then "this"
// child will be loaded with all nodes having a parent id of "this"
// child.
}
}
So you would start by taking your root-most id - I"m going to pretend that is "0" and call the recursive method...
LoadUpNodes(myTreeView.Nodes, "0");
So the "LoadUpNodes" method get's all of the children of "0".
When it goes to add the first child of "0" - let's pretend its ID is "1", then it calls itself like so:
LoadUpNodes(newChild.Nodes, "1");
Now it searches for all children of "1" and loades them into the node that is associated with the record id of "1" - (and doesn't load them to the root tree view.
Let's say the first child for record id "1" is id "2" - same thing, the function calls itself again:
LoadUpNodes(yetAnotherChild.Nodes, "2");
And all of "2"'s children are loaded up. - Now let's say the children of id "2" do not have any children themselves - at this point no more recursive calls would be made to LoadUpNodes and the call that was made to load the children of id "1" would continue to load the next child after id "2".
EDIT:
Now - you said that you were having trouble finding the "node" to load children into. I'm hoping this example demonstrates that you actually pass the child node that you created into the method recursively. By passing the child - that child now becomes the one that is having children loaded into it.
This is real confusing the first time you try to wrap your head around it, but once you get it, it's real easy.