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