Solved

How to add child node into TreeView?

Posted on 2009-04-07
9
416 Views
Last Modified: 2012-05-06
Hi, I currently have a Treeview which populates something like this
-X01
   -product
      -hardware
         - brand
              alpha
              beta
+X02
I'm connecting to another database for the last node.Due to the heavy load in the database i've decided to load it after the afterselect event.Like the code I've included.

What I aim to achieve is something like this...
-X01
   -product
      -hardware
         - brand
              -alpha
                  -alpha.101(read from new database)
                  -alpha.102(read from new database)
              beta
+X02

I hope someone can help me out.And i'm not very proficient in my programming so I'd appreciate it if your solution come in codes thanks in advance.
private void treeview1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level != 4)
            { return; }
            else
            {
                    localSQL = "SELECT KEYID,PRODUCT_NAME,PRODUCT_DESC,PKEYID,PRODUCT_TYPE FROM PRODUCT_PT WHERE PKEYID LIKE '" + treeview1.SelectedNode.Name + "'";
                    OracleCommand cmdOracle = new OracleCommand(localSQL, localConnection);
                    cmdOracle.CommandType = CommandType.Text;
                    OracleDataReader drO= cmdOracle.ExecuteReader();
                    while (drO.Read())
                    {
                    // Add node...???
                    }
                    drOracle.Close();
            }
        }

Open in new window

0
Comment
Question by:csharp_learner
[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
9 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 24085649
is this WPF ? then
while (drO.Read())
{
TreeViewItem Item = new TreeViewItem();
Item.Content = "Ur Text";
e.Node.Items.Add(Item);
}

Open in new window

0
 
LVL 6

Expert Comment

by:SaedSalman
ID: 24086812

while (drO.Read())
{
TreeViewItem item = new TreeViewItem();
Item.Content = "Text to add";  //if SELECT result then:drO[0].ToString()
treeview1.Node.Items.Add(item);
}

Open in new window

0
 

Author Comment

by:csharp_learner
ID: 24093332
Sorry to left  it out, but this is C# win forms.
I tried the following code but there are errors on the 3rd line...
                        TreeView item = new TreeView();
                        item = drOracle.GetOracleString(1).ToString();
                        treeview1.Node.item.Add(item);
0
Industry Leaders: 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!

 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 24093813

 item.Text = drOracle.GetOracleString(1).ToString();
0
 

Author Comment

by:csharp_learner
ID: 24093842
item.Text cannot be found in the list only Name can be found and i used it but line 3 is still in error.
Thanks for your reply.
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 24094959
treeview1.Nodes.Add(Item);
0
 

Author Comment

by:csharp_learner
ID: 24095028
I tried using
                            TreeView item = new TreeView();
                            item.Name = drOracle.GetOracleValue(4).ToString();
                            treeview1.Nodes.Add(item);

But 2 compile errors came out,both is based on line 3.

1.The best overloaded method match for 'System.Windows.Forms.TreeNodeCollection.Add(System.Windows.Forms.TreeNode)' has some invalid arguments.

2.Argument '1': cannot convert from 'System.Windows.Forms.TreeView' to 'System.Windows.Forms.TreeNode'
0
 
LVL 1

Accepted Solution

by:
prakhardeep earned 500 total points
ID: 24095144
You cannot add a treeview (line 1) to treenode (line 3).

Try the following:


TreeNode node = new TreeNode();
node.Text = drOracle.GetOracleValue(4).ToString();
e.Node.Nodes.Add(node);

Open in new window

0
 

Author Closing Comment

by:csharp_learner
ID: 31567412
Exactly what i was looking for Thanks.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

763 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