This is a migrated thread and some comments may be shown as answers.

Check node exist in treeview

13 Answers 714 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
SachinC
Top achievements
Rank 1
SachinC asked on 15 Oct 2010, 07:41 AM
Hello,

i am having a treeview. and adding the nodes manually.
For Each ct In ChecklistTemplate
  
                newChecklistTypeNode = New RadTreeNode(ct.ChecklistTypeName, False)
                newChecklistTypeNode.Tag = ct.CheckistTypeID
                newChecklistTypeNode.LoadedOnDemand = True
  
                If (Not tvChecklists.Nodes.Contains(newChecklistTypeNode)) Then
                    tvChecklists.Nodes.Add(newChecklistTypeNode)
                End If
Next

i am checking whether the node is already present in the treeview.
suppose following nodes are already added in treeview
1. abc
2. xyz
3. pqr

if i now add xyz node in treeview the condition tvChecklists.Nodes.Contains(newChecklistTypeNode) returns FALSE.

Please suggest.

Thanks
Sachin




13 Answers, 1 is accepted

Sort by
0
SachinC
Top achievements
Rank 1
answered on 15 Oct 2010, 07:58 AM
Found the solution
Change the code bit

Dim newChecklistTypeNode As RadTreeNode = Nothing
            For Each ct In ChecklistTemplate
  
                newChecklistTypeNode = New RadTreeNode(ct.ChecklistTypeName, False)
                newChecklistTypeNode.Tag = ct.CheckistTypeID
                newChecklistTypeNode.LoadedOnDemand = True
  
                If (tvChecklists.Nodes.Item(ct.ChecklistTypeName) Is Nothing) Then
                    tvChecklists.Nodes.Add(newChecklistTypeNode)
                End If
Next

Thanks!
:)


0
Emanuel Varga
Top achievements
Rank 1
answered on 15 Oct 2010, 08:05 AM
Hello Sachin,

This should work:
//treeView.Nodes.Find(string key, bool searchAllChildren)
treeView.Nodes.Find(key, true)

But I'm not 100%, usually, with trees, you have to parse them in order to find the node, something like this:
private bool NodeExists(RadTreeNodeCollection levelNodes, string key)
{
    var exists = false;
    foreach (RadTreeNode node in levelNodes)
    {
        if (node.Text == key)
        {
            exists = true;
            break;
        }
 
        exists = NodeExists(node.Nodes, key);
    }
 
    return exists;
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
Richard Slade
Top achievements
Rank 2
answered on 15 Oct 2010, 09:04 AM
Hi, 

I think you're right about having to parse them. The Telerik response to this question can be found in this code library area
Anyway, as far as I'm aware, a Find is usually doing the same thing under the hood. 
All the best
Richard
0
Emanuel Varga
Top achievements
Rank 1
answered on 15 Oct 2010, 09:08 AM
If it was working... just tried it, returns 0 elements all the time...

So either the method i suggested, or an extension method if it is needed in more than one class (to avoid duplicate code).

Best Regards,
Emanuel Varga
0
Stefan
Telerik team
answered on 20 Oct 2010, 11:13 PM
Hello guys, 

Currently, there are two ways to find a node. The first is the one that Richard has pointed out and the other one is described in our product documentation.

As to the Find method in RadTreeView that always returns 0 elements, this is a known issue that we are going to address in a future release. 

Should you guys need any assistance, do not hesitate to contact me.
 
Sincerely yours,
Stefan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Mark de Torres
Top achievements
Rank 1
answered on 25 Jan 2013, 07:45 AM
Hi Stefan,

The link is not working. Please advise


Richard Slade,

Your link is not working either. Please advise
0
Accepted
Princy
Top achievements
Rank 2
answered on 25 Jan 2013, 08:56 AM
Hi,

Take a look into this documentation for getting information about searching for a single node by its text.

Hope this helps.

Regards,
Princy.
0
Stefan
Telerik team
answered on 29 Jan 2013, 02:24 PM
Hi guys,

@Mark - please refer to the article Princy mentioned.

@Princy - thank you for helping our the community. I am updating your Telerik points for the efforts.
 
Regards,
Stefan
the Telerik team
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
0
Mark de Torres
Top achievements
Rank 1
answered on 02 Feb 2013, 11:49 AM
Hi Princy, Stefan

Thank you
0
David
Top achievements
Rank 1
answered on 15 Feb 2013, 05:44 AM
Hi Emanuel,

Thank you
0
Mark de Torres
Top achievements
Rank 1
answered on 15 Feb 2013, 07:11 AM
public EntityCollection<MusicEntity> GetMusicData()
{

    EntityCollection<MusicEntity> collection = GetMusicData();

   

    treMusic.Nodes.Clear();

    ArrayList ptnodes = new ArrayList();


    RadTreeNode nodeWorkplane = new RadTreeNode(p.PWork);
    RadTreeNode nodeUser = new RadTreeNode(p.UserProject.Items[0].UserName);

    RadTreeNode musicgroupnode = new RadTreeNode(p.MusicSubGroup.MusicGroup.Name, String.Empty);
    RadTreeNode musicsubgroupnode = new RadTreeNode(p.MusicSSubGroup.Name, String.Empty);
    RadTreeNode musicnode = new RadTreeNode(p.MusicTitle);
    musicsubgroupnode.Nodes.Add(musicnode);
    musicgroupnode.Nodes.Add(musicsubgroupnode);


    nodeUser.Nodes.Add(musicgroupnode);
    nodeUser.Nodes.Add(nodeWorkplane);


    pgnodes.Add(nodeUser);

    foreach (RadTreeNode pg in ptnodes) 
    {
        treMusic.Nodes.Add(pg);

    }

}

currently nodeUser the same user more than one.How i should prevent to add the same user node. please advise.
0
Stefan
Telerik team
answered on 19 Feb 2013, 08:02 AM
Hi David,

RadTreeView has a Nodes collection where the root nodes are placed and additionally each RadTreeNode also has Nodes collection for its child nodes, so you will need a recursive function to iterate the nodes. Here you can find a example: http://www.telerik.com/community/code-library/winforms/general/get-a-flattened-list-of-all-radmenuitems-in.aspx.

I hope this helps.

All the best,
Stefan
the Telerik team
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
0
Azhar
Top achievements
Rank 1
answered on 29 Mar 2018, 09:44 AM
Ok
Tags
Treeview
Asked by
SachinC
Top achievements
Rank 1
Answers by
SachinC
Top achievements
Rank 1
Emanuel Varga
Top achievements
Rank 1
Richard Slade
Top achievements
Rank 2
Stefan
Telerik team
Mark de Torres
Top achievements
Rank 1
Princy
Top achievements
Rank 2
David
Top achievements
Rank 1
Azhar
Top achievements
Rank 1
Share this question
or