Click here to Skip to main content
15,888,065 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have a tree view in which there are two child nodes with parentID 0 and a main node with no ID.


Root Node
0--> Child 1
1--> SubChilds
3--->SubChild1
1--> Subchilds
0--> Child 2
2--> SubChilds
2--> SubChilds

The tree structure is as above. The problem is that whenever I click on Child 2 the Child 1 node gets expanded, I have checked using breakpoints and found out that whenever I click on Child 2 the sleectednode.text shows Child 1. How can I clear this?

A piece of the code is given below :

------------------------------------------------
C#
Treeview1_SelectedNodeChanged()
{
Datatable dt = //Treeview binding
ds.tables.add(dt);
TreeNode selectedNode = New TreeNode();
selectedNode = this.Treeview1.SelectedNode.Text;
}

------------------------------------------------

Here selectedNode always shows the text of the first Child node even if I click on the second Child Node. I am binding the tree from the SQL table using a datatable.


Thank & Regards,
SaJ
Posted
Updated 3-Sep-15 23:10pm
v2
Comments
Abdul Samad KP 4-Sep-15 5:24am    
Why are you binding tree view in Selected node changed event?
Because of this, the selection is resting, that is why you are always getting first node.
By the way , why are you assigning a text to a tree node?
SajinAboo 4-Sep-15 5:29am    
Hi
Inside the selected node changed event i have created a method for binding a gridview, the grid view is binded based on the selected node's ID. After that only i have called the expansion code... Let me check if that was the issue.

Thanks & Regards,
SaJ
SajinAboo 4-Sep-15 5:35am    
Hi,

I have checked with that solution, but still it shows the same problem. Inside the Selected node changed event I have given
TreeView1.SelectedNode.Expand(), but the selected node shows the value of first child node not the node which I clicked...

Thanks & Regards,
SaJ
Abdul Samad KP 4-Sep-15 5:42am    
When are you binding the tree?
Inside page load?
SajinAboo 4-Sep-15 5:46am    
Yes, the treeview binding is done on page load.
I have created a method for creating the parent node and child node, the treeview works fine when the + sign is clicked, but upon selecting a child it always returns the value of the first child node.

1 solution

This happens because your treenodes have duplicate values, change the line

parentTreeNode.Value = firstDataRows["ParentId"].ToString();


to

parentTreeNode.Value = firstDataRows["Id"].ToString();


So that they will have unique values
 
Share this answer
 
Comments
SajinAboo 4-Sep-15 7:22am    
Also should i change it for the Child row method???
SajinAboo 4-Sep-15 7:36am    
Thank you, That worked like a charm...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900