如何通过后台工作程序将节点从数据库添加到 TreeView 并在 Windows 窗体 C# 中展开节点时重新加载子项?
我有一个 Nhibernate 数据库,它以分层列表的形式提供数据。我的Windows Forms GUI中有一个TreeView,还有一个Backgroundworker,它用所有根节点及其子节点填充TreeView(仅此而已,因为NHibernate的延迟加载异常,但这没关系,因为我预计用户将在TreeView中拥有许多节点)。
将根节点及其子节点添加到 TreeView 的过程运行得很好,但是当我单击一个节点来展开它时,应该从数据库加载子节点的所有子节点并将其添加到 TreeView 中。从数据库成功请求节点并将其存储在缓冲区列表中(当单击节点将其展开时)。
之后,我尝试了一些想法,例如重建 TreeView 并完全重新填充它,但通过此解决方案,我遇到了所有展开的节点都折叠的问题,因此我尝试存储展开的节点并且它有效。但我对这个解决方案不太满意,我觉得它可以更容易完成,因为我不想每次用户展开节点时都重新填充 TreeView。
如何从数据库重新加载 TreeNode 的子节点并将其显示在 TreeView 中,而无需重新填充整个 TreeView?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题应该为延迟加载提供一些帮助。
它使用线程池而不是后台工作者,你可以在谷歌上找到支持/反对的论点,但在我看来,在 Winforms 中使用线程池并不是一个坏选择。
根据上面的帖子的指导,将正在扩展的节点传递给工作人员。获得填充节点所需的数据后,请使用标准 如果##.InvokeRequired Pattern在节点上执行实际工作。
希望有帮助。
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.