L
liwowo
Unregistered / Unconfirmed
GUEST, unregistred user!
数据库结构:
mID mname parent
1 总部门 -1
2 部门1 1
3 部门2 1
4 部门3 1
5 分部门1 2
6 分部门2 2
7 分部门3 2
8 子部门1 5
9 子部门1 5
type
PMyRec = ^TMyRec; //定义指针
TMyRec = record
IName: string;
PName: string;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
TreeviewRefresh;
end;
function TForm1.DataSetToTreeNode(mDataSet: TDataSet;
mFieldNameParent: string; //父节点标识字段名
mFieldNameTreeText: string; //节点文本字段名
mFieldNameTreeId: string; //节点标识字段名
mTreeView: TTreeView; mTreeNode: TTreeNode;
mParentText: string): Boolean;
var
MyRecPtr: PMyRec;
vTreeNode: TTreeNode;
vFieldValues: Variant;
vFieldNames: string;
begin
New(MyRecPtr);
Result := False;
if not Assigned(mDataSet) then Exit;
if not Assigned(mTreeView) then Exit;
if not mDataSet.Active then Exit;
vFieldNames := Format('%s;%s;%s',
[mFieldNameParent, mFieldNameTreeText, mFieldNameTreeId]);
mDataSet.Filtered := False;
mDataSet.Filter := Format('%s=%s', [mFieldNameParent, QuotedStr(mParentText)]);
mDataSet.Filtered := True;
if mDataSet.RecordCount = 0 then Exit;
mDataSet.First;
while not mDataSet.Eof do begin
MyRecPtr^.IName := mDataSet.FieldbyName(mFieldNameTreeid).asstring;
MyRecPtr^.PName := mDataSet.FieldbyName(mFieldNameParent).asstring;
vTreeNode := mTreeView.Items.AddChildobject(mTreeNode,
trim(mDataSet.FieldByName(mFieldNameTreeText).AsString),MyRecPtr);
//以上加入treeview中,记录类型的也加入
vFieldValues := mDataSet[vFieldNames];
DataSetToTreeNode(mDataSet, mFieldNameParent, mFieldNameTreeText,
mFieldNameTreeId, mTreeView, vTreeNode,
mDataSet.FieldByName(mFieldNameTreeId).AsString);
///////Begin 恢复位置
mDataSet.Filtered := False;
mDataSet.Filter := Format('%s=%s', [mFieldNameParent, QuotedStr(mParentText)]);
mDataSet.Filtered := True;
mDataSet.Locate(vFieldNames, vFieldValues, []);
///////End 恢复位置
mDataSet.Next;
end;
Result := True;
end;
procedure TForm1.TreeviewRefresh ;
begin
treeview1.Items.Clear;
with adoquery1 do
begin
close;
sql.Clear;
sql.Add('select * from link');
open;
// if bof then exit;
end;
DataSetToTreeNode(adoquery1, 'parent', 'mname', 'mid',
TreeView1, nil, '-1');
adoquery1.close;
end;
//显示
procedure TForm1.TreeView1GetSelectedIndex(Sender: TObject;
Node: TTreeNode);
begin
if treeview1.Selected=nil then exit;
Label1.Caption := PMyRec(TreeView1.Selected.Data)^.IName + ' ' +
PMyRec(TreeView1.Selected.Data)^.PName;
end;
上面显的pmyrec记录不对
()中的是pmyrec记录下的的值
显示如下
总部门(1,-1)
|
部门1(1,4)[应为1,2] 部门2(1,4)[应为1,3] 部门3(1,4)[应为1,4]
|
分部门1(2,7)[应为2,5] 分部门2(2,7)[2,6] 分部门3(2,7)[2,7]
这是什么问题那位高手帮帮忙 我要在改treeview的同时更新数据库
没分了 希望大家
mID mname parent
1 总部门 -1
2 部门1 1
3 部门2 1
4 部门3 1
5 分部门1 2
6 分部门2 2
7 分部门3 2
8 子部门1 5
9 子部门1 5
type
PMyRec = ^TMyRec; //定义指针
TMyRec = record
IName: string;
PName: string;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
TreeviewRefresh;
end;
function TForm1.DataSetToTreeNode(mDataSet: TDataSet;
mFieldNameParent: string; //父节点标识字段名
mFieldNameTreeText: string; //节点文本字段名
mFieldNameTreeId: string; //节点标识字段名
mTreeView: TTreeView; mTreeNode: TTreeNode;
mParentText: string): Boolean;
var
MyRecPtr: PMyRec;
vTreeNode: TTreeNode;
vFieldValues: Variant;
vFieldNames: string;
begin
New(MyRecPtr);
Result := False;
if not Assigned(mDataSet) then Exit;
if not Assigned(mTreeView) then Exit;
if not mDataSet.Active then Exit;
vFieldNames := Format('%s;%s;%s',
[mFieldNameParent, mFieldNameTreeText, mFieldNameTreeId]);
mDataSet.Filtered := False;
mDataSet.Filter := Format('%s=%s', [mFieldNameParent, QuotedStr(mParentText)]);
mDataSet.Filtered := True;
if mDataSet.RecordCount = 0 then Exit;
mDataSet.First;
while not mDataSet.Eof do begin
MyRecPtr^.IName := mDataSet.FieldbyName(mFieldNameTreeid).asstring;
MyRecPtr^.PName := mDataSet.FieldbyName(mFieldNameParent).asstring;
vTreeNode := mTreeView.Items.AddChildobject(mTreeNode,
trim(mDataSet.FieldByName(mFieldNameTreeText).AsString),MyRecPtr);
//以上加入treeview中,记录类型的也加入
vFieldValues := mDataSet[vFieldNames];
DataSetToTreeNode(mDataSet, mFieldNameParent, mFieldNameTreeText,
mFieldNameTreeId, mTreeView, vTreeNode,
mDataSet.FieldByName(mFieldNameTreeId).AsString);
///////Begin 恢复位置
mDataSet.Filtered := False;
mDataSet.Filter := Format('%s=%s', [mFieldNameParent, QuotedStr(mParentText)]);
mDataSet.Filtered := True;
mDataSet.Locate(vFieldNames, vFieldValues, []);
///////End 恢复位置
mDataSet.Next;
end;
Result := True;
end;
procedure TForm1.TreeviewRefresh ;
begin
treeview1.Items.Clear;
with adoquery1 do
begin
close;
sql.Clear;
sql.Add('select * from link');
open;
// if bof then exit;
end;
DataSetToTreeNode(adoquery1, 'parent', 'mname', 'mid',
TreeView1, nil, '-1');
adoquery1.close;
end;
//显示
procedure TForm1.TreeView1GetSelectedIndex(Sender: TObject;
Node: TTreeNode);
begin
if treeview1.Selected=nil then exit;
Label1.Caption := PMyRec(TreeView1.Selected.Data)^.IName + ' ' +
PMyRec(TreeView1.Selected.Data)^.PName;
end;
上面显的pmyrec记录不对
()中的是pmyrec记录下的的值
显示如下
总部门(1,-1)
|
部门1(1,4)[应为1,2] 部门2(1,4)[应为1,3] 部门3(1,4)[应为1,4]
|
分部门1(2,7)[应为2,5] 分部门2(2,7)[2,6] 分部门3(2,7)[2,7]
这是什么问题那位高手帮帮忙 我要在改treeview的同时更新数据库
没分了 希望大家