473,670 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2603
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.co m

<mi************ @gmail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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....level N. 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(TreeN ode 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.co m

<mi************ @gmail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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....level N. 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(TreeN ode 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.co m
<mi************ @gmail.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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
2175
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 am able to check using xpath in asp.net 2.0 till MenuItem node. if i check visible attribute value till SubMenuLevel0 node then in tree it will not display the MenuItem Node at all Note: My tree Menu will start from MenuItem node and it will...
0
1161
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 am able to check using xpath in asp.net 2.0 till MenuItem node. if i check visible attribute value till SubMenuLevel0 node then in tree it will not display the MenuItem Node at all Note: My tree Menu will start from MenuItem node and it will...
3
2552
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 subdir only when its node on the tree control is opened. Has anyone pre-packaged this functionality into a control?
0
700
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 class as pGA. I pass data as treenode to createinitialpop method of pGA and read that tree through initialpopulation property of pGA. I then try to assign that tree to the treeview already placed on my form named tvwInitialPop. The...
0
8466
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8384
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8901
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8813
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8659
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4388
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2799
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1791
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.