Solved

How to add child node into TreeView?

Posted on 2009-04-07
9
404 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
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 24093813

 item.Text = drOracle.GetOracleString(1).ToString();
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now