在 TreeView 节点上存储数据的最佳方式是什么?

发布于 2024-07-13 19:49:40 字数 523 浏览 12 评论 0原文

TreeView 是向用户呈现层次结构的好方法,但想象一下具有如下所示层次结构的以下场景:

Building 1 
 -Tenant 1
   - Payment 1
   - Payment 2
Building 2
 -Tenant 1
   - Payment 1
 -Tenant 2
   - Payment 1
   - Payment 2

当用户单击“付款”节点时,您需要向数据库执行插入操作。 本质上,插入所需的变量是 Building_Id、Tenant_Id、Payment_Id。 组装这些的一种方法是走到每个节点的父节点:

Building_Id = Payment.ParentNode.ParentNode.Id

按照以下格式存储支付节点上的所有 id 值,然后解析 Building_Id、Tenant_Id、Payment_Id 的值是否更好? 例如:

Payment.Value = "1|2|1"

The TreeView is a nice way to present a hierarchy to users, but imagine the following scenario with the hierarchy depicted below:

Building 1 
 -Tenant 1
   - Payment 1
   - Payment 2
Building 2
 -Tenant 1
   - Payment 1
 -Tenant 2
   - Payment 1
   - Payment 2

where you need to do an insert to a database when the user clicks on the Payment node. Essentially the variables required for the insert are Building_Id, Tenant_Id, Payment_Id. One way to assemble these is to walk to the parent of each node:

Building_Id = Payment.ParentNode.ParentNode.Id

Is it better to store all of the values of the id's on the Payment Node in the following format, then parse the values for Building_Id, Tenant_Id, Payment_Id? For example:

Payment.Value = "1|2|1"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情徒 2024-07-20 19:49:40

我发现处理额外数据的最佳方法是子类化 TreeNode。 我创建一个 BaseNode 类,其中包含我想要维护的共享数据,并针对任何特定节点类型进一步继承该数据。

子类化的价值在于,您可以像任何其他类一样维护强数据类型和复杂数据类型......这避免了将数组黑客成带有管道分隔符等的字符串。

一旦你的节点就位,它就允许你建议的相同的树遍历,除了现在你从(比如说)BaseNode.MyData(你的所有子类型都将继承)中提取值。

不过,如果您这样做,需要注意一件事:您需要了解您希望这些节点具有多大的权威性。 就我而言,当用户导航树时,我们会检查数据库缓存以确保不需要重新填充数据。

I find the best way to handle additional data is to subclass TreeNode. I create a BaseNode class that contains the shared data I want to maintain, and inherit further from that for any specific node types.

The value of subclassing is that you can maintain strong data types and complex data types just like any other class... which avoids hacking arrays into a string with pipe separators and the like.

Once you have your nodes in place it allows the same tree walk you propose, except now you are pulling the values from (say) BaseNode.MyData (which all your subtypes will inherit).

One thing to watch for if you do this though: you need to understand how authoritative you want these nodes to be. In my case, when the user navigates the tree we check with a database cache to ensure we don't need to repopulate the data.

扛起拖把扫天下 2024-07-20 19:49:40

如果 TreeView 控件的 TreeNodes 具有保存对象的 Tag 属性,则可以将包含这些所需属性的自定义对象与每个 < code>TreeNode 的标签,然后您可以根据需要访问它们。

例如,在 .Net 4.5 中,它会是这样的:

myTreeNode.Tag = myObject;

其中 myTreeNodeTreeNodemyObject< 的实例/code> 是您定义的自定义对象的实例,其中包含您希望与 TreeViewTreeNode 关联的数据。

以下是 MSDN 上有关 TreeNode.Tag 属性的文章:MSDN - TreeNode.Tag 属性

If the TreeNodes of the TreeView control have a Tag property that holds an object, you can associate a custom object containing those desired properties with each TreeNode's tag, then you can access them as necessary.

For example, in .Net as of 4.5, it would be like this:

myTreeNode.Tag = myObject;

Where myTreeNode is an instance of TreeNode and myObject is an instance of the custom object you defined which contains the data you wish to have associated with a TreeNode of your TreeView.

Here is an article on MSDN on the TreeNode.Tag property: MSDN - TreeNode.Tag Property.

千紇 2024-07-20 19:49:40

您可能会考虑进一步采用 Godeke 的想法,而不是对 TreeNode 进行子类化,而是将节点绑定到业务对象集合 - 将本地数据存储在子集合的属性中。 收集逻辑将能够为您提供所需的数据,并且您可以获得将数据和逻辑与表示层分离的好处。

You might consider taking Godeke's idea further and instead of subclassing the TreeNode, tie the nodes to a business object collection - storing your local data in properties of the collection children. The collection logic will be able to give you the data you need and you gain the benefits of separating the data and logic from the presentation layer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文