在一个名为: Query1的TQuery控件中写入如下SQL:
select parent = (select max(nProcessID) from tProcess b where a.nProcessID like b.nProcessID + '%' and a.nProcessID <> b.nProcessID),
nProcessID as child, sProcessName
from tProcess a
order by Parent, Child
程序如下:
type
PCustomNode = ^TCustomNode;
TCustomNode = record
NodeKey, NodeValue, NodeName: String;
end;
function TForm1.findParent(ANodeKey: String): TTreeNode;
var
i: Integer;
begin
Result := nil;
for i := 0 to TreeView1.Items.Count - 1 do
if PCustomNode(TreeView1.Items[i].Data)^.NodeValue = ANodeKey then
begin
Result := TreeView1.Items[i];
Break;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
vNode: PCustomNode;
begin
Query1.Close;
Query1.Open;
with Query1 do
while (not IsEmpty) and (not Eof) do
begin
new(vNode);
查询语句:
select parent = (select max(nProcessID) from tProcess b where a.nProcessID like b.nProcessID + '%' and a.nProcessID <> b.nProcessID),
nProcessID as child, sProcessName
from tProcess a
var
strTmp:string;
tList:Tstring;
iPos,iIndex:integer;
begin
adoDataset.close;
adoDataset.commandtext:='select * from tname order by nProcessID';
adoDataset.open;
tList:=TStringList.Create;
while not adoDataset.eof do
begin
strTmp:=adoDataset.fields[0].asstring;
iPos:=pos('.',strTmp);
if iPos=0 then Node=nil
else
begin
strTmp:=copy(strTmp,0,iPos-1);
iIndex:=tnList.indexof(strTmp);
if iIndex=-1 then Node=nil
else node:=TTreeNode(tnList.objects[index]);
end;
这是两层的,另外,我假定你的数据是排好序的!
如多层,你就在这里想想
begin
strTmp:=copy(strTmp,0,iPos-1);
iIndex:=tnList.indexof(strTmp);
if iIndex=-1 then Node=nil
else node:=TTreeNode(tnList.objects[index]);
end;