Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > moving nodes up and down in a treeview
 

moving nodes up and down in a treeview

Hi All,

I have a treeview and many nodes and subnodes at each level.

I also have two buttons move_up and move_down.

for each selected node whenever we click the move_upor move_down button, we should be able to move the selected node up or down within the same level, ie under the same parent.

Could anyone provide me the code for the above

Thanks,

Jasna

jasna  Sunday, August 05, 2007 7:16 AM

We can loop through the childnodes in the current parent node to doinsert and remove operation, which makes the node move up and down,

Code Snippet

void button1_Click(object sender, EventArgs e)

TreeNode node = this.treeView1.SelectedNode.Clone() as TreeNode;

int index = -1;

for (int j = 0; j < this.treeView1.SelectedNode.Parent.Nodes.Count; j++)

if (this.treeView1.SelectedNode == this.treeView1.SelectedNode.Parent.Nodes[j])

break;

.treeView1.BeginUpdate();

this.treeView1.SelectedNode.Parent.Nodes.Insert(index-1, node);

this.treeView1.SelectedNode.Parent.Nodes.RemoveAt(index + 1);
.treeView1.EndUpdate();

this.treeView1.SelectedNode = node;.treeView1.Select();
private void button2_Click(object sender, EventArgs e)

TreeNode node = this.treeView1.SelectedNode.Clone() as TreeNode;

int index = -1;

for (int j = 0; j < this.treeView1.SelectedNode.Parent.Nodes.Count; j++)

if (this.treeView1.SelectedNode == this.treeView1.SelectedNode.Parent.Nodes[j])

break;

thisthis.treeView1.SelectedNode.Parent.Nodes.Insert(index + 2, node);

this.treeView1.SelectedNode.Parent.Nodes.RemoveAt(index);
.treeView1.EndUpdate();

this.treeView1.SelectedNode = node;
this

We can loop through the childnodes in the current parent node to doinsert and remove operation, which makes the node move up and down,

Code Snippet

void button1_Click(object sender, EventArgs e)

TreeNode node = this.treeView1.SelectedNode.Clone() as TreeNode;

int index = -1;

for (int j = 0; j < this.treeView1.SelectedNode.Parent.Nodes.Count; j++)

if (this.treeView1.SelectedNode == this.treeView1.SelectedNode.Parent.Nodes[j])

break;

.treeView1.BeginUpdate();

this.treeView1.SelectedNode.Parent.Nodes.Insert(index-1, node);

this.treeView1.SelectedNode.Parent.Nodes.RemoveAt(index + 1);
.treeView1.EndUpdate();

this.treeView1.SelectedNode = node;.treeView1.Select();
private void button2_Click(object sender, EventArgs e)

TreeNode node = this.treeView1.SelectedNode.Clone() as TreeNode;

int index = -1;

for (int j = 0; j < this.treeView1.SelectedNode.Parent.Nodes.Count; j++)

if (this.treeView1.SelectedNode == this.treeView1.SelectedNode.Parent.Nodes[j])

break;

thisthis.treeView1.SelectedNode.Parent.Nodes.Insert(index + 2, node);

this.treeView1.SelectedNode.Parent.Nodes.RemoveAt(index);
.treeView1.EndUpdate();

this.treeView1.SelectedNode = node;
this

You can use google to search for other answers

Custom Search

More Threads

• Changing the design-time dataset to match the real world.
• DtatGridView.RowValidating Event and changing is BindingSource's List
• Check new row is added
• DataGridView and Threads Problem
• Screen Print
• Databinding problem with DataGridView/DataTable - Why is the binding not done immediatly after setting DataGridView.DataSource?
• datagrid cannot add row
• How to detect CurrentItem change in BindingSource to show a Save button
• Inside datagridview - filter value of 1 combobox based on 2 combobox selected value
• Overwriting BindingNavigator AddNew