Solved

How to add child node into TreeView?

Posted on 2009-04-07
9
411 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best resource to learn entity framework 2 32
Google Directions API to Map URL -C#? 3 21
Hey!! 5 33
Release Dynamically Allocated Memory in C# 3 21
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  â€¦
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

786 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