Solved

How to stop TreeView repopulating the same node?

Posted on 2009-04-08
11
214 Views
Last Modified: 2012-05-06
Hi,
I have a TreeView which has 5 level of child nodes, the thing is I've placed an AfterSelect event after clciking on the 4th level to generate my 5th level of child nodes.But the problem is when ever I click on the same 4th level the same thing will generate again @ the 5th level.

How can I stop it from duplicating the same nodes?
My code for the AfterSelect Event is something like this.
//Connected to database
// Populate 5th level in TreeView
foreach (DataRow dr in ordt.Rows)
{
TreeNode newNode = new TreeNode();
newNode.Text =  dr[4].ToString();
 e.Node.Nodes.Add(newNode);
}

Open in new window

0
Comment
Question by:csharp_learner
  • 5
  • 4
  • 2
11 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 24103848
I believe one possible way is to check if a child node with the same text exists or something like that.

May a 4th level node have more than one child? If not - check number of children. If 0 - add, if 1 - skip.
0
 

Author Comment

by:csharp_learner
ID: 24103884
Unfortunately my 4th node does have more then 1 child.
Example:
-X01
   -product
      -hardware
         - brand
              -alpha
                  -alpha.101(read from new database)
                  -alpha.102(read from new database)
              -beta
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 24104034
OK, still it's not a problem.
May these children have same text?
What's the difference between them?
May be you need to set the Tag property (to some specific value)  when creating new nodes and later check that no children with the samer tag?

Another possible trick - to add the node to some "handled" list. If in the list - skipp adding nodes; not in the list - add nodes and add to the list.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 6

Expert Comment

by:HarryNS
ID: 24104050
Hey... You still have the same problem.

Can you give me your code.... I will get you the answer...
0
 

Author Comment

by:csharp_learner
ID: 24104158
Thanks for offering your help harry.
This is the AfterSelect event code.
private void treeview1_AfterSelect(object sender, TreeViewEventArgs e)
        {            localSQL = "SELECT KEYID,PRODUCT_NAME,PRODUCT_DESC,PKEYID,PRODUCT_TYPE FROM PRODUCT_PT WHERE PKEYID LIKE '" + treeScadaItems.SelectedNode.Name + "'";
            OracleCommand cmdOracle = new OracleCommand(localSQL, localConnection);
            cmdOracle.CommandType = CommandType.Text;
            OracleDataReader drOracle = cmdOracle.ExecuteReader(); 
            DataTable ordt = new DataTable();
            OracleDataAdapter orda = new OracleDataAdapter();
            orda.SelectCommand = cmdOracle;
            orda.Fill(ordt);
            drOracle.Close();
 
                    if (e.Node.Level == 4)
                    {
                        try
                        {
                            // Populate PRODUCT_PT level 5 in TreeView
                            foreach (DataRow dr in ordt.Rows)
                            {
                                string[] strlist = dr[1].ToString().Split('.');
                                TreeNode newNode = new TreeNode();
                                newNode.Text = strlist[5] + " ( " + dr[4].ToString() + " )";
                                e.Node.Nodes.Add(newNode);
                            }
                        }
                        catch (Exception newview)
                        {
                            MessageBox.Show(newview.ToString());
                        }
                    }

Open in new window

0
 
LVL 6

Expert Comment

by:HarryNS
ID: 24104266
This code works for me...
private void treeview1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 4)
            {
                localSQL = "SELECT KEYID,PRODUCT_NAME,PRODUCT_DESC,PKEYID,PRODUCT_TYPE FROM PRODUCT_PT WHERE PKEYID LIKE '" + treeScadaItems.SelectedNode.Name + "'";
                OracleCommand cmdOracle = new OracleCommand(localSQL, localConnection);
                cmdOracle.CommandType = CommandType.Text;
                OracleDataReader drOracle = cmdOracle.ExecuteReader();
                DataTable ordt = new DataTable();
                OracleDataAdapter orda = new OracleDataAdapter();
                orda.SelectCommand = cmdOracle;
                orda.Fill(ordt);
                drOracle.Close();
 
 
                TreeNode nodeCheck = null;
                TreeNode[] node = null;
                string strText = string.Empty;
                string[] strList = null;
 
                try
                {
                    // Populate PRODUCT_PT level 5 in TreeView
                    foreach (DataRow dr in ordt.Rows)
                    {
                        nodeCheck = new TreeNode();
                        nodeCheck.Name = dr[0].ToString();
                        strText = dr[2].ToString();
                        strList = strText.Split('.');
                        if (strList != null)
                        {
                            nodeCheck.Text = strList[strList.Length - 1];
                        }
                        else
                            nodeCheck.Text = strText;
                        if (dr[1] != DBNull.Value)
                        {
                            node = treeView1.Nodes.Find(dr[1].ToString(), true);
                            if (node != null && node.Length > 0)
                            {
                                node[0].Nodes.Add(nodeCheck);
                            }
                        }
                        else
                        {
                            e.Node.Nodes.Add(nodeCheck);
                        }
                    }
                }
                catch (Exception newview)
                {
                    MessageBox.Show(newview.ToString());
                }
            }
        }

Open in new window

0
 
LVL 6

Expert Comment

by:HarryNS
ID: 24104273
Please change Line No. 38 in my code
FROM: node = treeView1.Nodes.Find(dr[1].ToString(), true);
TO: node = e.Node.Nodes.Find(dr[1].ToString(), true);

0
 

Author Comment

by:csharp_learner
ID: 24104366
The code ran but nothing was displayed out on the TreeView...
0
 
LVL 6

Expert Comment

by:HarryNS
ID: 24104805
Will your Call to DB on Node 5 have sub items or not???
0
 
LVL 6

Accepted Solution

by:
HarryNS earned 500 total points
ID: 24104927
If you retrieve records to only one level on Level 5 of treeview then change this part,
foreach (DataRow dr in ordt.Rows)
                    {
                        nodeCheck = new TreeNode();
                        nodeCheck.Name = dr[0].ToString();
                        strText = dr[4].ToString();
                        strList = strText.Split('.');
                        if (strList != null)
                        {
                            nodeCheck.Text = strList[strList.Length - 1];
                        }
                        else
                            nodeCheck.Text = strText;
                        e.Node.Nodes.Add(nodeCheck);
                    }
0
 

Author Comment

by:csharp_learner
ID: 24105133
I fixed it using switch (e.Node.Level) and it always goes into the switch so it does'nt duplicate.
But thanks alot for helping out.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Not showing JavaScript in the list 5 52
Turn on intranet settings 1 55
User Authentication using Digital Certificate 2 44
What namespace do I need to import? 2 25
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!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

813 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

10 Experts available now in Live!

Get 1:1 Help Now