tags:

views:

79

answers:

2

I have three treeViews that have the same data loaded to them when a form is loaded. I can write the data in three different functions, but that seems like duplicating a lot of work. How can I load each treeView with the same data. So far I have tried the following and it does not work correctly. SecondRowNodes is a set of nodes I have loaded earlier in the function.

//this one misloads the first two and loads treeView4 correctly
TreeNode topNodes = new TreeNode(currentDataSet.HousingAreaTable[housingAreaCounter].HousingAreaName, secondRowNodes);
treeView2.Nodes.Add(topNodes);
treeView3.Nodes.Add(topNodes);
treeView4.Nodes.Add(topNodes);

//this one has the same effect as before and loads the first two wrong and treeView4 correctly
TreeNode topNodes = new TreeNode(currentDataSet.HousingAreaTable[housingAreaCounter].HousingAreaName, secondRowNodes);
TreeNode topNodes2 = new TreeNode(currentDataSet.HousingAreaTable[housingAreaCounter].HousingAreaName, secondRowNodes);
TreeNode topNodes3 = new TreeNode(currentDataSet.HousingAreaTable[housingAreaCounter].HousingAreaName, secondRowNodes);
treeView2.Nodes.Add(topNodes);
treeView3.Nodes.Add(topNodes2);
treeView3.Nodes.Add(topNodes3);
A: 

Have a function that takes the treeview as a parameter. It then just populates that treeview with the data, and you call it three times, once for each treeview.

You need to expand with a little more detail on your data model though.

Neil Barnwell
that does make it where I only have the code written once, which I am all for. I would however like to make it where the code only runs once too. I don't know if this is possible though.
Matt
Pass in an array of treeviews? I'm not sure what you're after, I don't think.
Neil Barnwell
A: 

You actually might want to take a look at a different control. I have been using ObjectListView for quite sometime for almost all of my listview and treeview needs. It allows you to populate a list by simply sending it any IEnumerable object. This means you only need to update one list and then call the SetObjects function for any or all of your TreeListView components. Take some time to look at the examples and cookbook at the website, because it is a very different way to handle lists. You can find the page and code at ObjectListView

Mark