0

I have a Nhibernate Database which provide the Data as hierarchical List<>. I have a TreeView in my Windows Forms GUI and a Backgroundworker which populate the TreeView with all Root Nodes and their Children (nothing more because of Lazy loading exception from NHibernate but this is okay because I expect that the user will have many Nodes in the TreeView).

The process to add the Root nodes and their children to the TreeView works pretty well but when I click on a Node to expand it all children of the children should be loaded from the Database and added to the TreeView. The Nodes are requested successfully from the Database and are stored in the buffer list (when Click on the node to expand it).

After that I tried a few ideas like rebuild the TreeView and repopulate it completely but with this solution I got the Problem that all expanded Nodes are collapsed so I tried to store the node that is expanded and it worked. But I am not quite happy with this solution, I got the feeling it can be done easier, because I don't want to repopulate the TreeView every time the User is expanding a Node.

How can I reload the Children of a TreeNode from the Database and display it in the TreeView without repopulating the whole TreeView?

Taryn
  • 242,637
  • 56
  • 362
  • 405
Daraku
  • 19
  • 6
  • The Lazy loading is not the Problem because I get the Children of the Root Nodes on initial loading from the Database. – Daraku Jun 09 '11 at 08:39

1 Answers1

0

This SO Question should provide some help on the lazy loading.

It uses a threadpool instead of a background worker which you can find arguments for/against on google, but in my opinion its not a bad choice to use the threadpool in Winforms.

With the above post to guide you, pass the node that is being expanded through to worker. Once you have the data required to populate the node, use the standard If ##.InvokeRequired Pattern to do the actual work on the node.

Hope that helps.

Community
  • 1
  • 1
Smudge202
  • 4,689
  • 2
  • 26
  • 44
  • the lazy loading wasn't a problem because I get the Children of the Root node the additional loading of the Children of the Children are the problem. (Didn't mention that in my Question, I will add it as a comment). But you gave me a Idea how to solve the problem with your "Invoke Required" Tip, I will look into it, thank you – Daraku Jun 09 '11 at 08:38
  • With regards to the lazy loading, the pattern suggested by [Fredrik Mörk](http://stackoverflow.com/users/93623/fredrik-mork) in the first post I linked is a very popular pattern. If you don't use it this time round, consider using it next time. ;-) Let me know if you want any more help with the InvokeRequired pattern. Good luck – Smudge202 Jun 09 '11 at 08:47