3

In my windows application I have a treeview. I made custum buttons to move a node downwards. This is what happens when a button is clicked:

Node destNode = tvCategories.SelectedNode.NextNode;
Node srcNode = tvCategories.SelectedNode;
Node parentNode = srcNode.Parent;

// Switch nodes
parentNode.Nodes[destNode.Index] = srcNode;
parentNode.Nodes[srcNode.Index] = destNode;

The code works fine, but my treeview isn't updated. I don't see the switch of the nodes.

tvCategories.Refresh() or tvCategories.Invalidate() or tvCategories.Update() doesn't work.

Anybody knows how to fix this?

PS: I'm using a 3rd party treeview of DevComponents.

Jehof
  • 33,666
  • 10
  • 120
  • 151
Martijn
  • 23,651
  • 59
  • 165
  • 253

2 Answers2

2

Setting Focus on the Treeview will cause a refresh as I have found by using

TreeView.Focus()

cheedep
  • 937
  • 5
  • 19
1

You can try to remove one node and to insert it again:

Node destNode = tvCategories.SelectedNode.NextNode;
// Check for null (what happens, if the last node is selected?)

// Switch nodes
destNode.Parent.Nodes.Remove( destNode );
destNode.Parent.Nodes.Insert( tvCategories.SelectedNode.Index, destNode );
tanascius
  • 51,705
  • 21
  • 111
  • 133
  • Thanks this way it works fine :) (there's a small bug in your code, it is destNode.Parent.Nodes.Remove()) – Martijn Feb 04 '10 at 10:51
  • @tanascius I usually don't do that but I'm totally stuck can you help me on that related question : http://stackoverflow.com/questions/26588519/c-sharp-treeview-doesnt-always-display-its-content Thanks – Thomas Ayoub Oct 28 '14 at 08:22