<p>I have an application that reads a table from a database. </p>

<p>I issue an SQL query to get a result set, based on a unique string value I glean from the results, I use a case/switch statement to generate certain objects (they inherit TreeNode BTW).  These created objects get shunted into a Dictionary object to be used later.</p>

<p>Whilst generating these objects I use some of the values from the result set to populate values in the object via the setters.</p>

<p>I query the Dictionary to return a particular object type and use it to populate a treeview.  However it is not possible to populate 2 objects of the same type in a treeview from the Dictionary object (you get a runtime error - which escapes me at the moment, something to with referencing the same object).  So what I have to do is use a memberwiseClone and implement IClonable to get around this.  </p>

<p>Am I doing this right?  Is there a better way - because I think this is causing my program to be real slow at this point.  At the very least I think its a bit clunky - any advice from people who know more than me - greatly appreciated.</p>

[{'answer_id': 56264, 'author': 'Brad Bruce', 'author_id': 5008, 'author_profile': 'https://Stackoverflow.com/users/5008', 'pm_score': 2, 'selected': False, 'text': '<p>Is there a reason you are using the external dictionary?  I would populate the tree directly as the data is queried.</p>\n\n<p>If you do require the dictionary, you could set the .Tag property of the tree node to point to the data in your dictionary.</p>\n'}, {'answer_id': 56299, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': 2, 'selected': True, 'text': '<p>To add to @<a href="https://stackoverflow.com/questions/56224/creating-objects-driven-by-the-database-to-populate-a-treeview-very-slow-c#56264">Brad</a>, only populate the tree as needed. That means hooking into the expand event of the tree nodes. This is similar to how Windows Explorer functions when dealing with network shares.</p>\n\n<p>There should be 1 <code>TreeNode</code> object per actual tree node in the tree - don\'t try to reuse the things. You may either associate them with your data using the Tag property (this is the recommended method), or you can subclass the <code>TreeNode</code> itself (this is the Java method, but used less in .NET).</p>\n\n<p>(The use of cloning methods is usually a hint that you\'re either (a) doing something wrong, or (b) need to factor your domain model to separate mutable objects from immutable.)</p>\n'}, {'answer_id': 3847349, 'author': 'GreyCloud', 'author_id': 397268, 'author_profile': 'https://Stackoverflow.com/users/397268', 'pm_score': 0, 'selected': False, 'text': "<p>have you considered using a Virtual Tree view which only loads the nodes the user actually wants to look at - i've had good success with the component from www.infralution.com </p>\n"}]
['https://Stackoverflow.com/questions/56224', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5175/']