Monday, April 17, 2017

VirtualStringTree as Grid - Delphi tutorial (Tip for 10 )


In this example you can learn how to use Virtualstringtree from GemSoft as a Grid.


This component can be used as a regular tree view but also as string grid,so it can replace regular stringgrid and dbgrid, but with much higher speed.
Not to mention customization.



Virtualstringtree is known for its speed, so adding 1000 or 10000 nodes takes a bit of a second.

In this tutorial you can learn how to variable columns and rows number, meaning to use variable data that should be shown, for example database table records etc.

Full source for this example can be found below

(code style formatted by http://hilite.me/ )

unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, VirtualTrees;

type
  TfrmMain = class(TForm)
    VST: TVirtualStringTree;
    txtColNumb: TEdit;
    txtRowNumb: TEdit;
    chkAddAsNew: TCheckBox;
    Label1: TLabel;
    Label2: TLabel;
    cmdAddData: TButton;

    procedure VSTColumnsCreate;
    procedure VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
    procedure cmdAddDataClick(Sender: TObject);

  private
    { Private declarations }
  public
  ColNumb,RowNumb:Integer;

  type
  PMyRec=^TMyRec;
  TstrArr=array of string;
  TMyRec=packed record
  ArrOfStr:TstrArr;
  end;

  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

procedure TfrmMain.cmdAddDataClick(Sender: TObject);
var
node:PVirtualNode;
MyRecord:PMyRec;
i,j:Integer;
begin
if StrToInt(txtColNumb.Text)=0 then  begin
VST.Clear;
Exit;
end;

ColNumb:=StrToInt(txtColNumb.Text);
RowNumb:=StrToInt(txtRowNumb.Text);

VSTColumnsCreate;

if chkAddAsNew.Checked=true then   VST.Clear;

for j := 1 to RowNumb do begin
  node:=VST.AddChild(nil);
  MyRecord:=VST.GetNodeData(node);
  SetLength(MyRecord.ArrOfStr,ColNumb);
  for i:=0 to ColNumb-1 do
  MyRecord.ArrOfStr[i]:='Field '+IntToStr(j)+':'+IntToStr(i);

end;
VST.EndUpdate;

end;

procedure TfrmMain.VSTColumnsCreate;
var i:Integer;
begin
VST.BeginUpdate;
with VST.Header do begin
  Columns.Clear;
  for i := 0 to ColNumb-1 do  begin
    Columns.Add;
    Columns[i].Width:=100;
    Columns[i].Text:='Column '+IntToStr(i);
  end;

end;
VST.EndUpdate;
end;

procedure TfrmMain.VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
  Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
  var
  MyRecord:PMyRec;
  i:Integer;
begin
MyRecord:=Sender.GetNodeData(Node);
for i := 0 to ColNumb-1 do begin

  if ( Column=i) then CellText:=MyRecord.ArrOfStr[i];

end;
end;

end.

0 comments:

Post a Comment

Popular Posts

Recent Posts

Unordered List

Text Widget

Pages

Search This Blog

Powered by Blogger.

Contributors

Text Widget