从数据库里把各个记录挂到treeview上的问题,建了数据库(id,父id,名称),可是在子节点找父节点时,很难做到,请指教!!
例如 数据库记录如下 结果应为
id pid name
1 0 a a
2 1 b b
3 2 c c
4 0 aa aa
5 0 aaa aaa
6 5 bbb bbb
7 5 ccc ccc 请指教代码,谢谢!!!!!
例如 数据库记录如下 结果应为
id pid name
1 0 a a
2 1 b b
3 2 c c
4 0 aa aa
5 0 aaa aaa
6 5 bbb bbb
7 5 ccc ccc 请指教代码,谢谢!!!!!
a : TTreeNode;
begin
a := TreeView1.Selected;
if a.Parent <> nil then
a := a.Parent; //移到父结点
end;
名 称:UpdateTree
主要功能:把数据集中的数据挂到树上;成功返加true,否则返加 false;
参数说明:aTree:TTreeView; 目标树控件
aDataSet:TADODataSet; 源数据集
aKeyField:string; 作为主键的字段(该字段必须唯一)
aParentField:string; 交节点字段
aListField:string 显示在节点text的字段 (注:)使用前参数所指数据集要打开,各参数所指字段要为string类型,父节点
要于aKeyField字段中存在;selectedIndex = 1,stateIndex = 0;根节点父节点
为'0';!!:使用该过程后,记得一定要释放树节点指针的内存(利用freeTree函数)
返 回 值:boolean
编写日期:2003-12-05
编写人员:[email protected]
*******************************************************************************}
function UpdateTree(aTree:TTreeView;aDataSet:TADODataSet;aKeyField:string;
aParentField:string;aListField:string): boolean;
var
arrParent: array of string;
lastNode: TTreeNode;
iParent: integer;
i: integer;
w,n: word;
//:此数据集用来查询
adsQue: TADODataSet;
pNode: PString;
sParent: string;
//:这三个变量用来验证三个参数字段是否合格
bp,bk,bl: boolean;
//:该函数判断当前记录是否已经挂在树上了
function checkExist(sText: string): integer;
var
i: integer;
xx: string;
begin
result := -1;
for i := 0 to aTree.Items.Count-1 do
begin
xx:=trim(string(aTree.Items.Item[i].Data^));
if trim(string(aTree.Items.Item[i].Data^)) = trim(sText) then
//:此记录已在树上
begin
result := i;
exit;
end;
end;
end;
begin
result := true;
bp := false;
bk := false;
bl := false;
if not aDataSet.Active then
begin
result := false;
MessageDlg('数据集没有打开!',mtError,[mbOK],0);
exit;
end;
if aDataSet.Filtered then
aDataSet.Filtered := false;
for i := 0 to aDataSet.FieldCount -1 do
begin
if (aDataSet.Fields.Fields[i].FieldName = aParentField) and
(aDataSet.Fields.Fields[i] is TStringField) then
bp := true;
if (aDataSet.Fields.Fields[i].FieldName = aKeyField) and
(aDataSet.Fields.Fields[i] is TStringField) then
bk := true;
if (aDataSet.Fields.Fields[i].FieldName = aListField) and
(aDataSet.Fields.Fields[i] is TStringField) then
bl := true;
end;
if not (bp and bk and bl) then
begin
result := false;
MessageDlg('字段名不存在或非字符串类型!',mtError,[mbOK],0);
exit;
end; adsQue := TADODataSet.Create(nil);
adsQue.Clone(aDataSet); aDataSet.First;
while not aDataSet.Eof do
begin
sParent := aDataSet.FieldByName(aParentField).AsString;
if not adsQue.Locate(aKeyField,sParent,[])
then if aDataSet.FieldByName(aParentField).AsString <> '0' then
begin
result := false;
MessageDlg('存在无效的父节点!'+ QuotedStr(sParent),mtError,[mbOK],0);
exit;
end;
aDataSet.Next;
end; if aDataSet.Locate(aKeyField,'0',[]) then
begin
result := false;
MessageDlg('节点值不能为''0''!',mtError,[mbOK],0);
exit;
end;
aDataSet.First;
while not aDataSet.Eof do
begin
if aDataSet.FieldByName(aParentField).AsString <> '0' then
begin
setLength(arrParent,1);
arrParent[0] := aDataSet.FieldByName(aParentField).AsString;
repeat
w := High(arrParent);
adsQue.Locate(aKeyField,arrParent[w],[]);
sParent := adsQue.FieldByName(aParentField).AsString;
for n := 0 to High(arrParent) do
begin
if arrParent[n] = sParent then
begin
result := false;
MessageDlg('存在循环父节点!' + QuotedStr(sParent),mtError,[mbOK],0);
exit;
end
else
begin
w := High(arrParent)+2;
setLength(arrParent,w);
arrParent[w-1] := sParent;
end;
end;
until adsQue.FieldByName(aParentField).AsString = '0';
end;
aDataSet.Next;
end; aTree.Items.Clear;
//:增加根节点
lastNode := aTree.Items.AddFirst(nil,'所有');
New(pNode);
pNode^ := '0';
lastNode.Data := pNode; with aDataSet do
begin
first;
while true do
begin
if checkExist(FieldByName(aKeyField).AsString) = -1 then
begin
if trim(FieldbyName(aParentField).AsString) <> '0' then
//:非根节点
begin
sParent := FieldByName(aParentField).AsString;
iParent := checkExist(sParent);
if iParent > -1 then
//:存在父节点
begin
lastNode := aTree.Items.AddChild(aTree.Items.Item[iParent],FieldByName(aListField).AsString);
New(pNode);
lastNode.Data := pNode;
string(lastNode.Data^) := FieldByName(aKeyField).AsString;
lastNode.StateIndex := 0;
lastNode.SelectedIndex := 1;
end;
end
else
//:根节点
begin
lastNode := aTree.Items.AddChild(aTree.Items.Item[0],FieldByName(aListField).AsString);
New(pNode);
lastNode.Data := pNode;
string(lastNode.Data^) := FieldByName(aKeyField).AsString;
lastNode.StateIndex := 0;
lastNode.SelectedIndex := 1;
end;
end;
if (aTree.Items.Count < aDataSet.RecordCount + 1) then
begin
if RecNo < RecordCount then
Next
else
First;
end
else
exit;
end;
end;
adsQue.Free;
end;