将新项目添加到树视图并保持树视图打开到正确的位置 [英] Adding New Items to a Tree View and keeping the Tree View Open to the right position

查看:81
本文介绍了将新项目添加到树视图并保持树视图打开到正确的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我能解释一下,这样才有意义.

我有一个TreeView,我将项目,子项目和子子项目的列表放入其中.它们分别是Timers,TagGroups和Tag.

现在,添加任何新内容时,我将从数据库中重新加载我的数据结构,然后清除并重新加载TreeView.结果是所有东西都关闭了,我必须重新展开我打开过的所有东西.

这在上下文中并不可怕,但是如果用户想将6个或8个标签添加到同一标签组中,显然不是特别好. (实际上,他们可以做到这一点而无需回到Tag Group……但是人性暗示他们还是会去的)

我想要保留在最后添加或编辑的项目上;即使我已清除树视图并重新创建它.

从数据库中提取树视图的内容时会对其进行排序,因此尽管我总是知道它的名称,但不能保证给定元素的位置保持在同一位置.

所以...关于如何最好地处理此问题的任何建议,以便最后添加/编辑的项目保持可见并被选中?代码示例也将不胜感激.

预先感谢.

I hope I can explain this so it makes sense.

I''ve got a TreeView that I put a list of items, sub-items, and sub-sub-items into. These are Timers, TagGroups, and Tags respectively.

Right now, when anything new is added, I reload my data structures from my DB and then clear and reload the TreeView. The result is that everything gets closed and I have to re-expand anything I had open.

This isn''t horrible in context but it obviously isn''t particularly nice if the user wants to add 6 or 8 tags to the same Tag Group. (In truth they can do that without going back to the Tag Group ... but human nature suggests they''re going to anyway)

What I would like is to remain on the item that was last added or edited; even though I''ve cleared the Tree View and recreated it.

The contents of the Tree View are sorted when pulled from the DB so the position of a given element isn''t guaranteed to remain in the same place ... although I will always know what it''s called.

So ... any recommendations about how to best handle this so the last added/edited item remains visible and selected? Code examples would also be appreciated.

Thanks in advance.

推荐答案

太糟糕了,我不知道您的数据库架构,但是还有一种更好的可能性:创建树的智能填充"看法.如果您在感觉到新的DataTables之后修改了数据填充的过程.做您以前的工作,仅不要重新创建树和树节点,将所需的数据与树视图中已经存在的数据(从根到所有子节点)进行调和.

—SA
Too bad I don''t know your database schema, but there is one more, better possibility: create "smart population" of the tree view. If you Modify the process of the population of the data after you feel new DataTables. Do what you did before, only do not re-create the tree and tree nodes, reconcile required data with the data already in the tree view, from the root down to all the child nodes.

—SA


这是我的c#代码,适用于多层次,希望您可以转换为VB.NET.

举例来说,我有一个名为产品"的表,数据列为"id"(原始和唯一?)&加载树视图时,将名称" ...等等等等...并将节点的键"元素设置为products-> id.

现在,我可以按照编程方式为给定的产品-> ID
选择一个节点
This is my c# code, works for me for multi levels and hope you can convert into VB.NET.

Say for instance, i have a table called "products", data columns "id" (primaty & unique?) & "name"...blah blah... and set the "Key" element of the node to products->id when loading the tree view.

Now I am able to programatically select a node as follows for a given products->ID

treeView1.SelectedNode =  treeView1.Nodes.Find(txtFind.Text, true).First() ;



进行更新时,希望您知道products-> id的值,并可以在更新语句后以编程方式找到并突出显示该节点,然后再次加载树形视图.

同样,当您执行插入操作时,您可以获取插入的ID并执行相同的操作.希望这会有所帮助,但要取决于您如何在系统中实现树状视图.


让我知道它是否有帮助



When you do the update, hope you know the products->id value and can find and highlight the node programatically after the update statement and loading the tree view again.

Similarly when you do an insert, you can get the inserted ID and do the same. Hope this helps but depends on how you have implemented the tree view in your system.


Let me know whether it helped


当我尝试此解决方案时:

When I tried this solution:

If NameToFind > "" Then
  For Each RootNode As TreeNode In TreeView.Nodes
    If RootNode.Text = MySelectedNode.Text Then TreeView.SelectedNode = RootNode
    For Each ChildNode As TreeNode In RootNode.Nodes
      If ChildNode.Text = MySelectedNode.Text Then TreeView.SelectedNode = ChildNode
      For Each GrandChildNode As TreeNode In ChildNode.Nodes
        If GrandChildNode.Text = MySelectedNode.Text Then TreeView.SelectedNode = GrandChildNode
      Next
    Next
  Next
End If



它几乎完成了我想要的一切. *唯一*的问题是它没有使节点突出显示.它选择了该节点,但未在实际的TreeView组件中显示突出显示.

有什么建议吗?



It did almost everything I wanted. The *only* problem was that it didn''t leave the node highlighted. It selected the node but didn''t display a highlight within the actual TreeView component.

Any suggestions?


这篇关于将新项目添加到树视图并保持树视图打开到正确的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆