Tree view population

  • Thread starter Thread starter mittal.pradeep
  • Start date Start date
M

mittal.pradeep

I have a table with 5 columns. Every column represents a level in a
hierarchy. I need to populate a windows forms tree view control using
this table.
Also Is there a recursive way to populate a tree view from a database
table with any number of levels. Please suggest if any one has some
pointers.
 
Using recursion for any number of levels is a bad idea. For large
numbers of levels, you run the risk of running out of stack space.

Rather, it is better if you iterate through the levels yourself, adding
as needed, without recursion.

If every column represents a level in the heiarchy, why not cycle
through the columns in the data set returned from the database and populate
the child nodes that way? This could easily be adapted for N levels.

Hope this helps.
 
Thanks a lot Nicholas.

I got a good direction. I should be not using recursion.

I should cycle through columns. But in this method, Is it possible to
have the adaptation to N level dynamic?

Specifically, Dynamicness is read from a table which provides me the
column names of level1, level2, level3....levelN. Whatever is the
number of levels specified in this table, Tree reads same number of
levels from database and gets populated.

If you can provide me any sample code links, I will be very thankful.

Thanks
PM
 
PM,

You could always do something like this:

void AddBranch(TreeNode root, DataColumn[] columns, DataRow data)
{
// Cycle through the columns. The columns are assumed
// to be the columns that represent the order of the heiarchy, so that
// columns[0] is the first node attached to root, columns[1] is
// a node attached to the node created by columns[0] and so on.
foreach (DataColumn column in columns)
{
// Add the tree node here. Use the data from the data row
// to get the data to place in the node.
// This assumes that the data in the data row can be anything.
// If you know the specific type, then you can adjust this
accordingly.
root = root.Nodes.Add(data[column].ToString());
}
}

Then, just cycle through your data table, and construct the columns
array, passing the current row, and the node you want to add this branch to.
 
Thanks again. But in this case won;t the number of nodes in level1 be
equal to number of rows in the data table. Suppose I have three rows in
my data table.

country state city
USA NJ jersey city
USA FL tempa
USA VT richmond

Then in the tree I will have three nodes at level 1 namely

USA
USA
USA


Shouldn't there be a single node named USA in this case.

Thanks,
PM
 
PM,

Honestly, I don't know, since you weren't too clear about the format of
^your^ data and how you wanted ^your^ tree to behave.

With what you said, I would adjust the code like this:

void AddBranch(TreeNode root, DataColumn[] columns, DataRow data)
{
// Cycle through the columns. The columns are assumed
// to be the columns that represent the order of the heiarchy, so that
// columns[0] is the first node attached to root, columns[1] is
// a node attached to the node created by columns[0] and so on.
foreach (DataColumn column in columns)
{
// Get the data for the column.
string val = data[column].ToString();

// The node.
TreeNode foundNode = null;

// See if it exists in the collection. If it does, then
// set the item to that. Otherwise, create a new one.
foreach (TreeNode node in root)
{
// If the text on the node is the same
// as your value, then use that.
if (node.Text == val)
{
// Set the found node to the node.
foundNode = node;
}
}

// If a node was found, then set the root to that node. Otherwise
// add the node to the root.
if (foundNode != null)
{
// Set the root to that node.
root = foundNode;
}
else
{
// Add the node.
root = root.Nodes.Add(val);
}
}
}
 
Back
Top