I have heard a lot of praise about the VirtualTreeView component and looked at using it in a rewrite we are doing. Currently we use a StringGrid.
I can't find a way to sort multiple columns, though single column sorting works great. Is there any way to do something similar to click column 1>sort, Ctrl+click column 2>sort column 2 after column 1, etc?
Specifically, I want to sort at least three columns: PO Number, Line Item, Release.
Thanks in advance for your help!
Here is the code (slightly simplified) I am testing the theory with (not from the same project referenced above):
Note: After your update I edited my code, also, to show what it currently is. Below I posted the results of the sort:
type
PBatchDetails = ^TBatchDetails;
TBatchDetails = record
TheBatchKey
OperationKey,
PO,
Line,
Release,
Temp,
Notes : String;
TransDate : TDateTime;
end;
....
Sorting_Columns: array of TColumnIndex;
....
procedure TForm1.TreeHeaderClick(Sender: TVTHeader; HitInfo: TVTHeaderHitInfo);
var
I: Integer;
begin
if not CtrlDown then //function I have to test Ctrl state.
begin
setlength(Sorting_Columns,0);
end;
SetLength(Sorting_Columns,length(Sorting_Columns)+1);
Sorting_Columns[Length(Sorting_Columns)-1] := HitInfo.Column;
tree.SortTree(HitInfo.Column,Sender.SortDirection,True);
if Sender.SortDirection=sdAscending then
Sender.SortDirection:=sdDescending
else
Sender.SortDirection:=sdAscending
end;
procedure TForm1.TreeCompareNodes(Sender: TBaseVirtualTree; Node1,
Node2: PVirtualNode; Column: TColumnIndex; var Result: Integer);
var
BatchRec1 : PBatchDetails;
BatchRec2: PBatchDetails;
I: Integer;
begin
if length(Sorting_Columns) > 0 then
begin
BatchRec1 := Tree.GetNodeData(Node1);
BatchRec2 := Tree.GetNodeData(Node2);
if (not Assigned(BatchRec1)) or (not Assigned(BatchRec2)) then
Result:=0
else
begin
for I := High(Sorting_Columns) downto 0 do
begin
case Sorting_Columns[i] of
0,1: Result := Result + CompareDate(BatchRec1.TransDate,BatchRec2.TransDate); // col 0 is Date and col 1 is Time.
2: Result := Result + CompareText(BatchRec1.OperationKey,BatchRec2.OperationKey);
3: Result := Result + CompareText(BatchRec1.PO,BatchRec2.PO);
4: Result := Result + CompareText(BatchRec1.Line,BatchRec2.Line);
5: Result := Result + CompareText(BatchRec1.Release,BatchRec2.Release);
6: Result := Result + CompareText(BatchRec1.Temp, BatchRec2.Temp);
7: Result := Result + CompareText(BatchRec1.Notes,BatchRec2.Notes);
end; //end case;
if Result <> 0 then
Break;
end;
end;
end;
end;
This produced the following results (I am only showing the three columns I am trying to sort here):
When originally loaded:
PO Line Release
153 7 2
153 7 1
153 1 1
153 1 2
153 4 1
153 6 2
153 6 1
120 3 2
120 3 1
153 2 1
153 4 2
120 2 1
153 4 1
120 1 1
153 3 1
153 2 1
111 2 1
111 1 5
111 1 1
111 4 2
111 3 1
111 4 1
111 1 3
111 1 2
111 1 4
After first click
PO Line Release
111 2 1
111 1 5
111 1 1
111 4 2
111 3 1
111 4 1
111 1 3
111 1 2
111 1 4
120 3 2
120 3 1
120 2 1
120 1 1
153 7 2
153 7 1
153 1 1
153 1 2
153 4 1
153 6 2
153 6 1
153 2 1
153 4 2
153 4 1
153 3 1
153 2 1
After second click
PO Line Release
153 7 2
153 7 1
153 6 2
153 6 1
153 4 1
153 4 2
153 4 1
111 4 2
111 4 1
153 3 1
120 3 2
120 3 1
111 3 1
153 2 1
153 2 1
120 2 1
111 2 1
153 1 1
153 1 2
120 1 1
111 1 5
111 1 1
111 1 3
111 1 2
111 1 4
After Third Click
PO Line Release
111 1 1
120 1 1
153 1 1
111 2 1
120 2 1
153 2 1
153 2 1
111 3 1
120 3 1
153 3 1
111 4 1
153 4 1
153 4 1
153 6 1
153 7 1
111 1 2
153 1 2
120 3 2
111 4 2
153 4 2
153 6 2
153 7 2
111 1 3
111 1 4
111 1 5
Thanks for your time!