如何在哈希表中将键值对显示为TreeView [英] How to display key value pairs in a hashtable as a TreeView

查看:123
本文介绍了如何在哈希表中将键值对显示为TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,键是服务器名称,值是该服务器的数据库...因此,如何将数据绑定到哈希表中,并且仅在TREE VIEW中...

In my project Key is the server name and value is the database of that server... So how to bind the data in hashtable and it will be in TREE VIEW only...

推荐答案

形式上,您可以执行此操作,但是该树将只有一个层次结构,所有键值对都位于一个根级别.你为什么要呢?这是因为键值对之间没有父子关系;至少您没有提供这种关系的定义.在这种情况下,自然的"亲子关系将看起来像没有父母或孩子,所有孩子都处于同一水平".确定要吗?继续阅读.

首先,不要使用System.Collections.Hashtable.从安全,可维护性和性能的角度来看,这将迫使您创建类型的案例,这是一个问题.该类为非泛型;在.NET Framework 2.0版中引入泛型时,它已由泛型类System.Collections.Generic.DictionarySystem.Collections.Generic.SortedDictionarySystem.Collections.Generic.SortedList取代.请参阅 http://msdn.microsoft.com/en-us/library/system.collections .generic.aspx [ ^ ].

要创建树视图,只需将每个System.Collections.Generic.KeyValuePair实例的实例用作树视图节点.可以添加任何类型的对象.唯一的问题是:每个节点的文本将显示什么?这不是非常有用的信息,因为控件将显示方法ToString返回的内容.要显示一些有用的信息,您应该创建一个包装器结构/类,其中包含一个KeyValuePair字段,并且还应覆盖object.ToString以便在树节点中提供一些有用的文本.该文本可能基于您的键,值或两者的值.

再说一遍,为什么您需要在列表中显示类似树的内容.使用ListBoxListView.基本上,与上述相同的想法仍然适用.

—SA
Formally, you can do it, but the tree will be of only one level of hierarchy, all key-value pairs on one root level. Why would you want it? This is because there is no parent-child relationships between Key-Value pairs; at least you did not provide a definition for such relationship. In this case, the "natural" parent-child relationships will looks like "there are no parents or child, all noted are at the same level". Are you sure you want it? Keep reading.

First of all, don''t use System.Collections.Hashtable. It will force you to make type cases, which is some problem from safety, maintainability and performance standpoints. This class in non-generic; it was superseded by generic classes System.Collections.Generic.Dictionary, System.Collections.Generic.SortedDictionary and System.Collections.Generic.SortedList when generics were introduced in the version 2.0 of .NET Framework. See http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx[^].

To make a tree view, just use the instance of each System.Collections.Generic.KeyValuePair instance as a tree view node. An object of any type can be added. The only problem is this: what will be displayed as a text of each node? Not very informative thing, as the control will display what the method ToString returns. To display something informative, you should create a wrapper structure/class which has a KeyValuePair field in it and also overrides object.ToString to provide some informative text in the tree node. This text could be based on the values of your Key, Value or both.

Then again, why would you need to present something like a tree where a list would do. Use ListBox or ListView. Basically, the same idea as the one described above still apply.

—SA


这篇关于如何在哈希表中将键值对显示为TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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