473,410 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

Tree view population

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.

Nov 17 '05 #1
5 2588
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<mi************@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.

Nov 17 '05 #2
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

Nov 17 '05 #3
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<mi************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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

Nov 17 '05 #4
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

Nov 17 '05 #5
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);
}
}
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
<mi************@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tree menu using XML | last post by:
I have one XML file that has nodes and sub node and each and every node has the attribute call visible if its value is true then diplay this node else don't display thid node, but this condition i...
0
by: Tree menu using XML | last post by:
I have one XML file that has nodes and sub node and each and every node has the attribute call visible if its value is true then diplay this node else don't display thid node, but this condition i...
3
by: _DS | last post by:
Problem with mapping a directory tree to a tree control: It takes a while to recurse subdirs and map them to nodes. This is solved in some books I've seen (MacDonald, Albahari) by reading the...
0
by: Muhammad Shabbir | last post by:
I faced problem similar to that of Loren Baker. I am developing a course timetable using Genetic Algorithm approach. I have cretaed a class named primitiveGA and instantiated an object of that...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.