Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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...
Posted
Updated 26-Aug-11 1:06am
v2
Comments
OriginalGriff 8-Aug-11 3:20am    
Sorry? That doesn't make a lot of sense. Use the "Improve question" widget to edit your question and provide better information.
Sergey Alexandrovich Kryukov 26-Aug-11 10:43am    
Well, quite formally it can be done, but the tree would be quite special -- all nodes at one level. So, I provided the solution, please see.
--SA
Baji Jabbar 11-Aug-11 4:41am    
Use a class to hold the key and value as a pair
Sergey Alexandrovich Kryukov 26-Aug-11 10:44am    
Right, and what?.. please see my solution.
--SA
pooja 08 26-Aug-11 7:38am    
can not make tree view with hashtable. you can add unique value as key in hashtable and in server we can have multiple database. so please check your logic. or improve your question

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900