1

I have a TVirtualTreeview in my application (still D2007 based) which sets RootNodeCount to the desired value and in OnInitNode event I assign an Object to the node which is otherwise being stored in a TObjectList in my busines logic class. The object is taken from the business logic class by the Nopde's ID which acts as index into the TObjectList.

Now I added sorting capabilities to the VST which for instance meant to add a comparison method to my business logic class which takes the objects attached to the two nodes the OnCompareNodes event gets passed. So far so good, the VST itsself sorts.

Since I'd like the business logic to have the same sorting as that can print or export data and it should do so in the same way it is being displayed I thought I'd use the TObjectList's .exchange method and simply give it the ID's of the two nodes passed to OnCompareNodes.

That fails. The output I get and the debugger supports this is quite different. I'm quite puzzled about the possible reasons. of course I could implement sorting of the TObjectList in my business logic class separately and apply this at the end of the OnHeaderClick event, but that's some additional effort I'd like to avoid.

Any ideas about what I might do wrong or where I'm wrong?

ain
  • 22,394
  • 3
  • 54
  • 74
Markus
  • 63
  • 6
  • Could you rephrase this question? Right now I understand you want to sort your ObjectList by employing the TreeView (which is not a VST, isn't it?); that surely isn't what you do, do you? – NGLN Dec 20 '14 at 16:07
  • From here it's difficult to tell how did you implemented this pattern, but it sounds like you are missing some sort of [`CollectionView`](http://msdn.microsoft.com/en-us/library/vstudio/system.windows.data.collectionview%28v=vs.110%29.aspx) class. A class that would be a layer between the VT and your source collection (and would know the order of your object items). VT in this scenario can work as a *real* virtual control - their nodes has no relation to the data. – TLama Dec 20 '14 at 16:16
  • About the idea of a CollectionView: means my business logic would be responsible for the order of the items and the OnCompareNode of VST would never be used instead I'd tell my business logic how to sort based on the sort column etc. I find when OnHeaderClick is being called? I'd clear the VST then and simply set RootNodeCount again so it redraws? Is this the idea? – Markus Dec 20 '14 at 19:54
  • Yes. The main point is that your source collection keeps unchanged. It would be the collection view's responsibility to prepare the items for the tree as they will be seen (where items are just pointers to the source collection items, optionally sorted, filtered or grouped somehow). Tree itself would just khow how many *visible* items are in that collection view (`RootNodeCount`). Tree then asks e.g. for text by the `OnGetText` event in which then asks the collection view data e.g. by the `Node.Index`. Sorting is then like (in pseudo-code) `Tree.BeginUpdate; Collection.Sort; Tree.EndUpdate;`. – TLama Dec 20 '14 at 20:48

1 Answers1

0

Ok, the answer is that the business logic manages everything and the VST in GetText etc. gets the required data directly from the bussines logic via the Node.Index. Then business logic can do the sorting as it likes.

Markus
  • 63
  • 6