unit VTDBExample;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
VirtualTrees, StdCtrls, ExtCtrls, Provider,
DB, DBClient,
Menus, ImgList, Grids, DBGrids, ADODB, BetterADODataSet;
type
TBasicNode =
class
protected
FCaption : shortstring;
FID : longint;
FImageIndex : longint;
FHasChanged : boolean;
public
constructor Create( const sCaption : shortstring; const iID, iIndex: longint);
property Caption : shortstring read FCaption write FCaption;
property ID : longint read FID write FID;
property ImageIndex : longint read FImageIndex write FImageIndex;
property HasChanged : boolean read FHasChanged write FHasChanged;
end;
TBasicNodeAdd =
class(TBasicNode)
protected
FJobTitle : shortstring;
FAdd1 : shortstring;
FAdd2 : shortstring;
FAdd3 : shortstring;
public
property Add1 : shortstring read FAdd1 write FAdd1;
property Add2 : shortstring read FAdd2 write FAdd2;
property Add3 : shortstring read FAdd3 write FAdd3;
property JobTitle : shortstring read FJobTitle write FJobTitle;
end;
PTreeData = ^TTreeData;
TTreeData = record
FObject : TBasicNode;
end;
type
TfrmVTDBExample =
class(TForm)
Label1: TLabel;
cds: TClientDataSet;
Panel1: TPanel;
VT: TVirtualStringTree;
imgMaster: TImageList;
cds3: TBetterADODataSet;
ADOConnection1: TADOConnection;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure VTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
var Text: WideString);
procedure VTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
procedure VTInitNode(Sender: TBaseVirtualTree; ParentNode, Node: PVirtualNode;
var InitialStates: TVirtualNodeInitStates);
procedure FormActivate(Sender: TObject);
procedure VTGetImageIndex(Sender: TBaseVirtualTree;
Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
var Ghosted: Boolean; var ImageIndex: Integer);
procedure VTCompareNodes(Sender: TBaseVirtualTree; Node1,
Node2: PVirtualNode; Column: TColumnIndex; var Result: Integer);
procedure VTGetNodeDataSize(Sender: TBaseVirtualTree; var NodeDataSize: Integer);
procedure VTInitChildren(Sender: TBaseVirtualTree; Node: PVirtualNode; var ChildCount: Cardinal);
private
procedure LoadCDS;
end;
implementation
{$R *.DFM}
constructor TBasicNode.Create( const sCaption : shortstring; const iID, iIndex: longint);
begin
inherited Create;
FCaption := sCaption;
FID := iID;
FImageIndex := iIndex;
end;
const
FLDN_CustNo = 0;
FLDN_Company = 1;
FLDN_Addr1 = 2;
FLDN_Addr2 = 3;
FLDN_City = 4;
FLDN_State = 5;
FLDN_Zip = 6;
FLDN_Country = 7;
FLDN_Phone = 8;
FLDN_FAX = 9;
FLDN_TaxRate = 10;
FLDN_Contact = 11;
FLDN_LastInvoiceDate = 12;
procedure TfrmVTDBExample.FormActivate(Sender: TObject);
var
r : TRect;
begin
SystemParametersInfo(SPI_GETWORKAREA, 0, @r, 0);
Height := r.Bottom-Top;
Width := r.Right-Left;
Application.ProcessMessages;
LoadCDS;
end;
procedure TfrmVTDBExample.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
procedure TfrmVTDBExample.LoadCDS;
var
Node : PVirtualNode;
begin
with cds do
begin
VT.BeginUpdate;
try
FileName := 'customer.cds';
Active := True;
IndexFieldNames := 'CustNo';
while not eof do
begin
Node := VT.AddChild(nil);
VT.ValidateNode( Node, False );
Next;
end;
finally
VT.EndUpdate;
end;
end;
end;
procedure TfrmVTDBExample.VTGetNodeDataSize(Sender: TBaseVirtualTree; var NodeDataSize: Integer);
begin
NodeDataSize := SizeOf(TTreeData);
end;
procedure TfrmVTDBExample.VTInitNode(Sender: TBaseVirtualTree; ParentNode, Node: PVirtualNode;
var InitialStates: TVirtualNodeInitStates);
var
Data : PTreeData;
iImage : integer;
begin
Data := Sender.GetNodeData(Node);
iImage := -1;
if Sender.GetNodeLevel( Node ) = 0 then
begin
iImage := 3;
if (Node.Index < 10) then
begin
iImage := 31;
InitialStates := InitialStates + [ivsHasChildren];
end;
end;
Data.FObject := TBasicNodeAdd.Create( cds.Fields[FLDN_Company].AsString, cds.Fields[FLDN_CustNo].AsInteger, iImage );
end;
procedure TfrmVTDBExample.VTInitChildren(Sender: TBaseVirtualTree; Node: PVirtualNode; var ChildCount: Cardinal);
begin
ChildCount := 1;
end;
procedure TfrmVTDBExample.VTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
var
Data: PTreeData;
begin
Data := Sender.GetNodeData(Node);
Data.FObject.Free;
Finalize( Data^ );
end;