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
  • 34,674
  • 10
  • 123
  • 155
Martijn
  • 24,441
  • 60
  • 174
  • 261

2 Answers2

2

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

TreeView.Focus()

cheedep
  • 937
  • 6
  • 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
  • 53,078
  • 22
  • 114
  • 136
  • 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