Monday, March 26, 2012

permanently Removing a node from a treeview?

hi all

i have managed to remove a node from a treeview by pressing a button, however it doesnt remove it from the database and when i re-run the program the node is still there. Can anyone please help me?

Granto

During your deletion of the node, you'll need to run a delete statement against the table as well. Then refresh your datatable, dataset, or whatever you are using and rebind to your treeview. Should work fine.


Hi,

This problem which you have posted at:http://forums.asp.net/t/1191381.aspx

If the answers in above thread have helped you out, please mark this post as Resoved. If not, don't hesitate to let me know.

However, I provide some suggestions here. Before you remove the node, you should delete the datain database first. Because only remove nodes won't delete the datain database. You can follow the below codes.protected void RemoveNodeButton_Click(object sender, EventArgs e) { TreeNode nd=TreeView1.SelectedNode;if (nd.Parent ==null) {//you should find which are the children node of it. DeleteNode(nd); TreeView1.Nodes.Remove(nd);//which is parent node }else {//delete the data in database by "nd.Text", such as: delete from test where t1='"+nd.Text+"'" TreeView1.SelectedNode.Parent.ChildNodes.Remove(nd);//which is child node } }//delete the every child of the node.protected void DeleteNode(TreeNode nd) {if (nd.ChildNodes.Count == 0)// It is the deepest node. {//Delete the data of child node in database by "nd.Text", such as: delete from test where t1='"+nd.Text+"'"return; } TreeNodeCollection nds = nd.ChildNodes;foreach (TreeNode ein nds) { DeleteNode(e); }//Delete the data of parent node in database by "nd.Text", if you need to delete the data about parent node. Such as: delete from test where t1='"+nd.Text+"'"return; }

Hope this can help you.


cheers for the feedback, could you give an example with code i would need to use?

Granto

0 comments:

Post a Comment