Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TreeView Child Node Population

Posted on 2008-06-25
3
Medium Priority
?
1,317 Views
Last Modified: 2008-06-26
Please See Code Snippet for Question:
//I have a TreeView that I am dynamically filling with values from a //database. First, when the page loads I call a function to fill the //initial server node: 
 
private void FillServStatusList()
		{
			MySelectCommand.CommandText =  "Select Status_Desc from Server_Status ORDER BY Server_Status_ID";
			dreader = MySelectCommand.ExecuteReader();
			cmbServStatus.Items.Clear();
			while (dreader.Read())
			{
				cmbServStatus.Items.Add(dreader["Status_Desc"].ToString());
			}
			dreader.Close();
		}
 
//Next, when the user clicks the node, I call another function: 
private void tvwServDB_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
            FillServerNode(tvwServDB.SelectedNode);
		}
 
		private void FillServerNode(TreeNode myNode)
		{
			//MySelectCommand.CommandText =  "nsp_get_database @server_name = '" + myNode.Text + "'";
            MySelectCommand.CommandText = "uspGetServerNode @StatusDesc = '" + myNode.Text + "'";
			dreader = MySelectCommand.ExecuteReader();
			while (dreader.Read())
			{
                TreeNode thisNode = myNode.Nodes.Add(dreader["Server_Name"].ToString());
				// WorkNode.ForeColor = tvwServDB.SelectedNode.ForeColor;
				//thisNode.ForeColor = System.Drawing.Color.Black;
			}
			dreader.Close();
		}
 
//This populates the nodes with child info. How can I add to this so that I can had some child, child rows?

Open in new window

0
Comment
Question by:computerstreber
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 total points
ID: 21871667
There are at least two ways, hard to say wich one suites you.

First way is to add child nodes to an existing child as soon as this child is created. In your while loop you do something like:

                        while (dreader.Read())
                        {
                               TreeNode thisNode = myNode.Nodes.Add(dreader["Server_Name"].ToString());
                                thisNode.Nodes.Add("child of child");// you need to figure out what to add to this specific node
                                // WorkNode.ForeColor = tvwServDB.SelectedNode.ForeColor;
                                //thisNode.ForeColor = System.Drawing.Color.Black;
                        }



Second way is to find a node you want to add children to after all children are added to the root.
You just iterate through children nodes of the root and select one based on some your criteria (text, tag, something else?), and finally add children to this root child.
0
 
LVL 3

Author Comment

by:computerstreber
ID: 21879232
You solution works, however, I am looking for how to add additional nodes based off of database output. For example, my database has a status table, a server table, and a database table. All servers must have one status and databases must be assigned to at least one server. Thus, my initial query sets the status nodes, then when the node is selected a function is called that populates those nodes with the corresponding server. Now, when either a node is clicked or when the server codes get populated I want to fill the server nodes with all the databases that reside on that particular server. How can I implement that?
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 21880679
Honestly, not enough information to tell exactly how to do.

As I understand you want to populate nodes when user clicks (selects)  the node.
There are some complications. Easy one - if the node has no children at the moment - there is no "+" sign next to it. really it is quite common situatiuon. What people do is they add some senseless dummy node as a child node to a normal, say, "server",node. And when this "server" node is expanded the dummy node should be removed.

How to populate... Not very sure. I'd try the following approach. When creatin these, e.g.,  "server" nodes I'd attach some information to them as a tag (can be any object) , for example, server name. When node is clicked we take this server name, generate sql query (use precompiled one), run query and populate the node.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question