1

i have a treeview like this below
i use c# for winform programming

  1. MONITOR
    • LG
    • Samsung
    • HP
  2. KEYBOARD
    • HP
    • Dell
      • Black
      • White
  3. MOUSE

so can u tell me how can i programmically change places of 1st and 2nd nodes in this example.

namco
  • 6,208
  • 20
  • 59
  • 83

5 Answers5

6

Simply get the reference of node you want to move, remove it and then insert it on required index

//get the ref
TreeNode node = treeView.Nodes[0];
//remove
treeView.Nodes[0].Remove();
//insert
treeView.Nodes.Insert(1, node);
Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
2

Here is a compilable example. I implemented the node exchanger as an extension method of TreeView. Note the BeginUpdate ... EndUpdate calls. Required references: System.Core.dll, System.Drawing.dll and System.Windows.Forms.dll.

using System;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;

public static class ExtensionMethods
{
  public static void ExchangeRootNodes(this TreeView treeView, string key1, string key2)
  {
    treeView.BeginUpdate();
    try {
      int i1 = treeView.Nodes.IndexOfKey(key1);
      if (i1 == -1)
        throw new ArgumentException("No node with this key: " + key1, "key1");
      int i2 = treeView.Nodes.IndexOfKey(key2);
      if (i2 == -1)
        throw new ArgumentException("No node with this key: " + key2, "key2");
      if (i1 == i2)
        return;
      var node1 = treeView.Nodes[i1];
      var node2 = treeView.Nodes[i2];
      node1.Remove();
      node2.Remove();
      if (i1 < i2) {
        treeView.Nodes.Insert(i1, node2);
        treeView.Nodes.Insert(i2, node1);
      } else {
        treeView.Nodes.Insert(i2, node1);
        treeView.Nodes.Insert(i1, node2);
      }
    } finally {
      treeView.EndUpdate();
    }
  }
} 

public static class Program 
{
  public static void Main() 
  {
    var form = new Form() { Left = 100, Top = 100, ClientSize = new Size(220, 300), Text = "Node Exchange Test" };

    var treeView = new TreeView() { Left = 10, Top = 10, Width = 200, Height = 245 };
    form.Controls.Add(treeView);
    treeView.BeginUpdate();
    try {
      treeView.Nodes.Add("MONITOR", "MONITOR");
      treeView.Nodes[0].Nodes.Add("LG");
      treeView.Nodes[0].Nodes.Add("Samsung");
      treeView.Nodes[0].Nodes.Add("HP");
      treeView.Nodes.Add("KEYBOARD", "KEYBOARD");
      treeView.Nodes[1].Nodes.Add("HP");
      treeView.Nodes[1].Nodes.Add("Dell");
      treeView.Nodes[1].Nodes[1].Nodes.Add("Black");
      treeView.Nodes[1].Nodes[1].Nodes.Add("White");
      treeView.Nodes.Add("MOUSE", "MOUSE");
      treeView.ExpandAll();
    } finally {
      treeView.EndUpdate();
    }

    var button = new Button() {Left = 10, Top = 265, Width = 200, Height = 25, Text = "MONITOR <-> KEYBOARD" };
    form.Controls.Add(button);
    button.Click += delegate(object sender, EventArgs e) {
      try {
        treeView.ExchangeRootNodes("MONITOR", "KEYBOARD");
      } catch (ArgumentException exception) {
        MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
    };

    form.Visible = true;
    Application.Run(form);
  }
}
kol
  • 27,881
  • 12
  • 83
  • 120
0

I assume you are in Windows Forms.

look at this answer: C# - TreeView: inserting node at certain position you should insert the nodes in the position you want them to be.

in your case you could insert the node KEYBOARD in position 0 and the MONITOR node will probably move down to position 1, but you could do this when building the tree the first time directly.

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • hi @Davide Piras can we add another field to the products table named PositionId and can list product and its sublist according to the PositionId is that aBetter Idea? – Sreenath Plakkat Dec 07 '11 at 10:01
0

The clean way is to sort the tree with your custom TreeViewNodeSorter. For example you can write a sort order in the Tag property of each tree node and use it for sorting the tree.

Alexander
  • 3,724
  • 8
  • 42
  • 50
0
int indCurr = treeView.SelectedNode.Index;
int levelCurr = treeView.SelectedNode.Level;
if (indCurr == 0) return;

if (levelCurr == 0)
{
  TreeNode prevNode = treeView.Nodes[indCurr - 1];
  prevNode.Remove();
  treeView.Nodes.Insert(indCurr, prevNode);
}
else
{
  TreeNode prevNode = treeView.SelectedNode.Parent.Nodes[indCurr - 1];
  prevNode.Remove();
  treeView.SelectedNode.Parent.Nodes.Insert(indCurr, prevNode);
}
namco
  • 6,208
  • 20
  • 59
  • 83