两个表的数据同TREEVIEW联接?(100分)

  • 主题发起人 主题发起人 lyplay2
  • 开始时间 开始时间
L

lyplay2

Unregistered / Unconfirmed
GUEST, unregistred user!
表一(难点是表的层次不能确定)
单位编号 单位名称 父单位编号
01 A公司 0
0101 一分厂 01
010101 一门市 0101
0102 二分厂 01
02 B公司 0
。。。。。。。。。。。。
表二(同表一以单位编号联接)
单位编号 姓名 性别
01 AAA A
01 BBB A
0101 CCC A
010101 DDD B
0102 EEE B
02 FFF A
02 GGG A
显示为
01 A公司
--0101 一分厂
------------------AAA
------------------BBB
----010101 一门市
----------------------CCC
--0102 二分厂
-----------------------EEE
02 B公司
--GGG

有什么好办法让两者能正确反应层次及关系?
速度当然越快好有什么好的算法吗?

 
根据编码长度在前面补空格
 
倒不是格式的问题
是如何创建树的问题!!!
 
办法是有,也很简单,不过你的数据表设计得太不规范了,有很多得隐患,
我建议你还是从新设计数据表!
 
对单位表按单位编号排序,然后增加节点并保存节点,增加下一个节点的时候看单位编号
是不是属于上个节点中单位编号(用Copy函数比较),假如不属于那就与上个节点的
父节点的单位编号比较,如此直到找到或到根节点为止
 
下面供你参考,有全局变量和结构体没有定义,
function Tfrm_jsgz.SearchParentNode(StartNode: TTreeNode;
bh: string): TTreeNode;
begin
Result := TreeView.Items[0];
if Assigned(StartNode) and (TreeView.Items[0] <> StartNode) then
begin
if Copy(bh, 1, length(PNodeData(StartNode.Data)^.bh)) = PNodeData(StartNode.Data)^.bh then
Result := StartNode
else
Result := SearchParentNode(StartNode.Parent, bh);
end;
end;

procedure Tfrm_jsgz.CreatePerson(Node: TTreeNode);
begin
with ClientDataSet_ryzl, TreeView.Items do
begin
Filter := 'f_bmid=' + IntToStr(PNodeData(Node.Data)^.id);
while not eof do
begin
with AddChild(Node, Fields[1].AsString) do
begin
ImageIndex := 2;
SelectedIndex := 2;
StateIndex := Fields[0].AsInteger;
end;
next;
end;
end;
end;

procedure Tfrm_jsgz.CreateTree;
var
Node, CurrNode: TTreeNode;
sDw: string;
P: PNodeData;
I, J, H: Integer;
begin
for I := 0 to TreeView.Items.Count - 1 do
if Assigned(TreeView.Items[0].Data) then
begin
Dispose(TreeView.Items[0].Data);
TreeView.Items[0].Data := nil;
end;
TreeView.Items.Clear;

if sDwmc <> '' then
sDw := sDwmc
else
sDw := '【未设单位】';

new(P);
TreeView.Items.AddChildObject(nil, sDw, TObject(P)).ImageIndex := 0;
OpenSql('select f_id,f_bh,f_mc from t_bm order by f_bh', True, Query_Temp);
CurrNode := nil;

with Query_Temp, TreeView.Items do
begin
while not eof do
begin
new(P);
P^.id := Fields[0].AsInteger;
P^.bh := Fields[1].AsString;
P^.Filter := 'f_bmid = ' + Fields[0].AsString;
P^.InStr := Fields[0].AsString;
CurrNode := AddChildObject(SearchParentNode(CurrNode,
Fields[1].AsString), Fields[2].AsString, P);
CurrNode.ImageIndex := 1;
CurrNode.SelectedIndex := 1;
CreatePerson(CurrNode);
Next;
end;
Close;
end;
end;
 
>>来自:NeutronDelphi, 时间:2002-12-4 12:59:00, ID:1484432
>>办法是有,也很简单,不过你的数据表设计得太不规范了,有很多得隐患,
>>我建议你还是从新设计数据表!
很简单,能说出来听听吗?
能提供个规范点的数据表供参考吗?不好意思我菜鸟一个:)

 
太简单了,
没有人愿意答
我胡捣了一下午
还是不行[:(]
 
给我分,我帮你搞定,你觉的可以的话,和我联系h1yn@sohu.com
 
treeview
Treeview1.items.addchild(Treeview1.selected,Tstring)


with Treeview1.items.addchild(Treeview1.selected,caption) do
begin
table1.edit;
table1.fieldbyname('').AsString:='String';
table1.post;
end;
Couldn't perform the edit because another user changed the record.

if (Treeview1.Selected <> Nil) then
Treeview1.Selected.Delete;

-------------------------------------------------------------------------------------------------------------------------
for i := 0 to mainf.TV_gdwj.items.count - 1 do
if mainf.TV_gdwj.items.item.Text=Cds_wsgdwj.fieldbyname('jg').asstring then
mainf.TV_gdwj.items.item.selected:=true;

------------------------------------------------------------------------------------------------------------------------

id parent_id caption code
35 23 XXXX 20010002
36 35 0003


XXXX

XXXXcode
type//,
Treeifor = ^TMyRec;
TMyRec = record
ID : string;//id
caption : string;//
code:String;
Tag : Integer;
end;
,
1:
var
temp:TtreeNode;
WZInfo:Treeifor;
begin
New(WZInfo);
WZInfo^.ID:=table.FieldValues['id'];
WZInfo^.code:=table.FieldValues['code'];
WZInfo^.caption:=table.FieldValues['caption'];
WZInfo^.Tag:=namber;
//
temp:=TreeView.Items.AddChildObject(TreeNode//,WZInfo^.caption,WZInfo);
//
(TreeNode)TreeNodeData
aNode.Data := WZInfo;.
:
//code:Treeifor(TreeView1.Selected.TreeNode.Data)^.code
//ID:Treeifor(TreeView1.Selected.Data)^.ID
//caption:Treeifor(TreeView1.Selected.Data)^.caption
,
-------------------------------------------------------------------------------------------

Area
AreaID0101010102
Area

type
TAreaID = record
ID: String[20];
Len: Integer;
end;
PAreaID = ^TAreaID;

TreeViewData
with TQuery.Create(Self) do
begin
DatabaseName := 'BDETable';
SQL.Add('select * from Area');
SQL.Add('where LEN(AreaID) = :AreaLen');
SQL.Add('order by AreaID');
ParamByName('AreaLen').AsInteger := 2;
Open();
while not(Eof) do
begin
tmpNode := ZoneTree.Items.Add(nil, FieldByName('Area').AsString);
new(AreaData);
AreaData.ID := FieldByName('AreaID').AsString;
AreaData.Len := 2;
tmpNode.Data := AreaData;
Next();
end;
Close();
for i := 2 to 5 do
begin
ParamByName('AreaLen').AsInteger := i * 2;
Open();
while not(Eof) do
begin
tmpNode := nil;
for j := ZoneTree.Items.Count - 1 downto 0 do
begin
AreaData := ZoneTree.Items[j].Data;
if Copy(FieldByName('AreaID').AsString, 1, AreaData.Len) = AreaData.ID then
begin
tmpNode := ZoneTree.Items[j];
Break;
end;
end;
tmpNode := ZoneTree.Items.AddChild(tmpNode, FieldByName('Area').AsString);
new(AreaData);
AreaData.ID := FieldByName('AreaID').AsString;
AreaData.Len := i * 2;
tmpNode.Data := AreaData;
Next();
end;
Close();
end;
Free();
end;
------------------------------------------------------------------------------------------------------------------


2
4

PB
ware_code=len(warecode)
if ware_code=2 then
//
ltvi_item.label = warename
ltvi_item.data = warecode
ltvi_item.Pictureindex = 1
ltvi_item.selectedpictureindex = 1
ltvi_item.children = true
ll_newitem = tv_dw.insertitemlast(0,ltvi_item)

fetch c1 into :warecode,:warename;
elseif ware_code=4 then
//+
ltvi_item.label = warename
ltvi_item.data = warecode
ltvi_item.Pictureindex = 2
ltvi_item.selectedpictureindex = 2
ltvi_item.children = true
ll_tvi = tv_dw.insertitemlast(ll_newitem,ltvi_item)
fetch c1 into :warecode,:warename;
else
ltvi_item.label = warename
ltvi_item.data = warecode
ltvi_item.Pictureindex = 3
ltvi_item.selectedpictureindex = 3
ltvi_item.children = true
ll_tv2 = tv_dw.insertitemlast(ll_tvi,ltvi_item)
fetch c1 into :warecode,:warename;
end if
-------------------------------------------------------------------------------------------------------------------
XYXYHXYM ZYZYHZYM BJBJHBJM
XSXHXMBJH
TREEVIEWDBGRIDTreeView


99
99

99
99
TreeView




treeView,
TreeView



treeView
procedure TForm1.Button1Click(Sender: TObject);
var
xsh,zyh:string;
xsNode,zyNode:TTreeNode;
begin
TreeView1.Items.Clear;
ADOQuery1.First;

while not ADOQuery1.Eof do
begin
xsh:=trim(ADOQuery1.FieldByName('XSH').AsString);
xsNode:=TreeView1.Items.Add(nil,ADOQuery1.FieldByName('XSM').AsString);
//
ADOQuery2.Close;
ADOQuery2.SQL.Clear;
ADOQuery2.SQL.Add('select * from BZY where XSH='''+xsh+'''');
ADOQuery2.Open;
ADOQuery2.First;
while not ADOQuery2.Eof do
begin
zyh:=trim(ADOQuery2.FieldByName('ZYH').AsString);
zyNode:=TreeView1.Items.AddChild(xsNode,ADOQuery2.FieldByName('ZYM').AsString);

//
ADOQuery3.Close;
ADOQuery3.SQL.Clear;
ADOQuery3.SQL.Add('select * from BBJ where ZYH='''+zyh+'''');
ADOQuery3.open;
ADOQuery3.First;
while not ADOQuery3.Eof do
begin
TreeView1.Items.AddChild(zyNode,ADOQuery3.FieldByName('BJ').AsString);
ADOQuery3.Next;
end;
// End

ADOQuery2.Next;
end;

ADOQuery1.Next;
end;

end;
---------+++++++++++++++++++++++++++++++++++
Object
Object

type
PInfoId = ^TInfoId;
TInfoId = Record
Id: string;
end;
IdInfo: TInfoId;
//
New(IdInfo);
IdInfo^.Id := MDetailQy['MInfoId'];
LevelThreeNode := MTypeTView.Items.AddChildObject(LevelTwoNode,
MDetailQy['MInfoName'] + '(' +
MDetailQy['Model'] + ')',
IdInfo);
//
PInfoId(MTypeTView.Selected.Data)^.Id

+++++++++++++++++++++++++++++++++++++++
(Treeview)Win95(Common ControlCOMCTL32.DLL)Delphi2.0TreeviewOutline
Treeview(Windows95)Treeview(TreeNode)TtreeNodeTTreeview(Text)(Data)TextStringData(Untyped Pointer)
ItemsIndex(TreeNodeIndex)Items01IndexOf(AbsoluteIndex)Treeview0ItemIndexIndexCountMoveToItem
Expanded()TrueIsVisibleItemHasChildren GetFirstChild, GetLastChild, GetPrevChild, and GetNextChildGetNextSibling and GetPrevSiblingLevelGetNextVisible and GetPrevVisibleParentHasAsParentTrue. ParentFocusedFocus SelectedDropTarget

.1.
Items
(Treeview.Selected = nil)
AddFirst, AddFirstChild, AddChildTreeview.Items.AddFirst( nil, 'Root')



Treeview.Selected.Delete


Treeview.Selected.EditText

(TTreeNode.Parent= nil)
Treeview
TreeView.Items.BeginUpdate;

TreeView.Items.EndUpdate


2.
Treeview
SelectedIndexTimageList
OverlayIndexX
ImageIndex
StateIndex StateImagesImageList-1
TreeviewFormImageListIndex0,1,TreeviewImageImageListTreeNodeImageIndex(Selected=nil)SelectedIndex

3.Level
Level
Level0 Level1 Level2

4.
SortType
TreeView.AlphaSortCustomSort

5.Drag&Drop
+++++++++++++++++++++++++++++++++++++++++++++++=

1.
procedure TForm1.TreeView1Click(Sender: TObject);
var
t: TTreeNode;
begin
if TreeView1.Selected = nil then exit;
t := TreeView1.Selected;
if t.Level<=2 then exit;
repeat
t := t.Parent;
until t.Level = 2;
Caption := t.Text;
end;

2.1repeat
3.PopupMenuOnPopup


LGXing, 2002-11-14 9:15:00, ID1431002
1theNode.Parent.Parent.Parent
2
3
case TV.Selected.Level of
0:popmenu0.popup(...);
...
end;

++++++++++++++++++++++++++++++++++++++++++++++++
TreeView1
AdoQuery1.Close;
AdoQuery1.Sql.Clear;
AdoQuery1.Sql.Add("select user from user");
AdoQuery1.open;
var
TreeRoot:TTreeNode;
begin
TreeRoot:=TreeView1.items.Add('ROOT');
while not AdoQuery1.eof do
begin
TreeView1.items.AddChild(TreeRoot,AdoQuery1.fieldbyname('user').asstring);
AdoQuery1.next;
end;
end;
.......
imagelist,treeviewimagesimagelist

node := treeview.item.addfirst(nil,'');
treeview.item.addchile(node,‘’);
++++++++++++++++++++++++++++++++++++
Delphi 5
1.
type
PMyRec = ^TMyRec;
TMyRec = record
FName: string;
LName: string;
end;
2.
ObjectTTreeNodes
procedure TForm1.Button1Click(Sender: TObject);
var
MyRecPtr: PMyRec; //
TreeViewIndex: LongInt;
begin
New(MyRecPtr); //
//
MyRecPtr^.FName := Edit1.Text;
MyRecPtr^.LName := Edit2.Text;
TreeViewIndex := StrToInt(Edit3.Text);
with TreeView1 do
begin
if Items.Count = 0 then
Items.AddObject(nil, 'Item' + IntToStr(TreeViewIndex), MyRecPtr)
else if (TreeViewIndex < Items.Count) and (TreeViewIndex >= 0) then
Items.AddObject(Items[TreeViewIndex], 'Item' + IntToStr(TreeViewIndex), MyRecPtr);
end;
end;
3.
:
procedure TForm1.Button2Click(Sender: TObject);
begin
Label1.Caption := PMyRec(TreeView1.Selected.Data)^.FName + ' ' +
PMyRec(TreeView1.Selected.Data)^.LName;
end;


doll_paul, 2002-11-3 19:35:00, ID1410825
gz


hq_pan, 2002-11-3 19:41:00, ID1410833
(TreeNode)TreeNodeData
aNode.Data := MyRecPtr;
Data

function funFreeTreeViewNodeData(aNode: TTreeNode): integer;
var
p: Pointer;
begin
Result := -1;
if Assigned(aNode.Data) then
begin
try
p := aNode.Data;
Dispose(p);
aNode.Data := nil;
except
on E: Exception do
Application.MessageBox(PChar(E.Message),
'(Data)', 0);
end;
end;
Result := 1;
end;
TTreeViewOnDeletionfunFreeTreeViewNodeData(Node);
OnDeletion
++++++++++++++++++++++++++++++++++++++++++++
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};

{$R *.RES}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
=======================================================================
Unit1.pas:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, ComCtrls, StdCtrls;

type
TForm1 = class(TForm)
pnlLeft: TPanel;
tvTest: TTreeView;
btnCreateNodes: TButton;
edtCount: TEdit;
UpDown1: TUpDown;
edtCaption: TEdit;
pnlRight: TPanel;
memTVNodes: TMemo;
btnListNodes: TButton;
procedure btnCreateNodesClick(Sender: TObject);
procedure tvTestDeletion(Sender: TObject; Node: TTreeNode);
procedure btnListNodesClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.btnCreateNodesClick(Sender: TObject);
var
pstrNC: array of PString;
aNode: TTreeNode;
s: String;
i, iCount: Integer;
begin
iCount := UpDown1.Position;
SetLength(pstrNC, iCount);
aNode := tvTest.Items.GetFirstNode;
for i := 0 to iCount - 1 do
begin
s := edtCaption.Text + ' - ' + IntToStr(i);
GetMem(pstrNC, Length(s));
pstrNC := PString(s);
tvTest.Items.AddChildObject(aNode, s, pstrNC);
end;
ShowMessage('Add Nodes OK!');
end;

procedure TForm1.tvTestDeletion(Sender: TObject; Node: TTreeNode);
begin
if Assigned(Node.Data) then
begin
FreeMem(Node.Data);
end;
end;

procedure TForm1.btnListNodesClick(Sender: TObject);
var
s: String;
rootNode, curNode: TTreeNode;
begin
rootNode := tvTest.Items.GetFirstNode;
if rootNode.HasChildren then
begin
memTVNodes.Lines.Clear;
curNode := rootNode.getFirstChild;
while curNode <> Nil do
begin
s := curNode.Text;
if curNode.Data <> Nil then
s := s + ' (' + String(PString(curNode.Data)) + ')';
memTVNodes.Lines.Add(s);
curNode := rootNode.GetNextChild(curNode);
end;
ShowMessage('Get All Nodes!');
end
else ShowMessage('The TreeView Has No Nodes!');
end;
end.
====================================================================
Unit1.dfm:
object Form1: TForm1
Left = 282
Top = 148
Width = 629
Height = 444
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = ''
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 12
object pnlLeft: TPanel
Left = 0
Top = 0
Width = 185
Height = 417
Align = alLeft
BevelInner = bvRaised
BevelOuter = bvLowered
TabOrder = 0
object tvTest: TTreeView
Left = 2
Top = 2
Width = 181
Height = 413
Align = alClient
Indent = 19
TabOrder = 0
OnDeletion = tvTestDeletion
Items.Data = {
01000000250000000000000000000000FFFFFFFFFFFFFFFF0000000000000000
0C546573742050537472696E67}
end
end
object btnCreateNodes: TButton
Left = 200
Top = 80
Width = 89
Height = 33
Caption = 'CreateNodes'
TabOrder = 1
OnClick = btnCreateNodesClick
end
object edtCount: TEdit
Left = 200
Top = 48
Width = 65
Height = 20
TabOrder = 2
Text = '10'
end
object UpDown1: TUpDown
Left = 265
Top = 48
Width = 15
Height = 20
Associate = edtCount
Min = 0
Position = 10
TabOrder = 3
Wrap = False
end
object edtCaption: TEdit
Left = 200
Top = 16
Width = 89
Height = 20
TabOrder = 4
Text = 'edtCaption'
end
object pnlRight: TPanel
Left = 368
Top = 0
Width = 253
Height = 417
Align = alRight
BevelInner = bvRaised
BevelOuter = bvLowered
TabOrder = 5
object memTVNodes: TMemo
Left = 2
Top = 2
Width = 249
Height = 413
Align = alClient
Lines.Strings = (
'memTVNodes')
TabOrder = 0
end
end
object btnListNodes: TButton
Left = 200
Top = 128
Width = 89
Height = 33
Caption = 'ListNodes'
TabOrder = 6
OnClick = btnListNodesClick
end
end


twos, 2002-11-3 22:49:00, ID1411060

node.data=aPointer
apointer
apointer
PMyData=^TMyData;
TMyData=record
aInt: Integer;
aStr: String[20];
aChr: Char;
end


hq_pan, 2002-11-4 12:54:00, ID1411697

procedure TForm1.tvTestDeletion(Sender: TObject; Node: TTreeNode);
begin
if Assigned(Node.Data) then
begin
FreeMem(Node.Data);
Node.Data := Nil; //Add this
end;
end;


+++++++++++++++++++++++++++++++++++++++




 
type //在类中先定义好
Groupifo = ^TMyRec;
TMyRec = record
Name: string;
DepartID: string;
end;

//初始化部门树 你可以先添加两层,在窗体创建时调用这个函数
procedure InitDeptTreeView(TreeView:TTreeView;);
var FirstItem,Temp:TtreeNode;
DeparInf:Groupifo;
Begin
TreeView.Items.Clear ;
FirstItem:=TreeView.Items.Add(nil,'单位信息');
Query1.Close;
Query1.Sql.Clear;
Query1.Sql.Add('SELECT * FROM 表名 WHERE 父单位编号='0');
Query1.Open;
Query1.First;
while Not Query1.Eof Do
begin
New(DeparInf);
if Not Query1.FindField(单位编号).IsNull Then
DeparInf^.DepartID:=Query1.FieldValues [单位编号];
if Not Query1.FindField(单位名称).IsNull Then
DeparInf^.Name:=Query1.FieldValues [单位名称];
Temp:=TreeView.Items.AddChildObject(FirstItem,DeparInf^.Name,DeparInf);
FindChoseBoyItem(Temp,TreeView);
Query1.Next ;
End;
Query1.Close ;
End;

////在当前选中部门上创建子部门
procedure FindChoseBoyItem(ChoseItem:TTreeNode;TreeView:TTreeView);
var
DeparInf:Groupifo;
Begin
Query.Close;
Query.Sql.Clear;
Query.Sql.Add('SELECT * FROM 表名 WHERE 父单位编号=Groupifo(ChoseItem.Data)^.DepartID);
Query.Open;
Query.First;
If Query.RecordCount<>0 Then
Begin
While Not Query.Eof do
Begin
New(DeparInf);
if Not Query1.FindField(单位编号).IsNull Then
DeparInf^.DepartID:=Query1.FieldValues [单位编号];
if Not Query1.FindField(单位名称).IsNull Then
DeparInf^.Name:=Query1.FieldValues [单位名称];
TreeView.Items.AddChildObject(ChoseItem,DeparInf^.Name,DeparInf);
Query.Next ;
End;
End
Else
Query.Close ;
End;


在TreeView1的onChange事件中写
procedure TForm1.TreeView1Change(Sender: TObject;Node: TTreeNode);
Var
Item: TTreeNode;
Begin
If Node.Level<>0 Then
Begin
DeptID1:=Groupifo(NodeData)^.DepartID;
If Node.SelectedIndex<>1 Then
Begin
Item:=Node.getFirstChild;
While Item<>Nil Do
Begin
FindChoseBoyItem(Item,TreeView1);
Item := Item.getnextsibling;
End;
End;
End;
End;
 
有种很简单的方法,非常非常好用,
先用select语句得到如下结构,
Child, Parent, Descr
之后,用dxDBTreeList之类的控件,设置几个属性就搞定,
高效,快捷,好用,稳定
 
to kingson
什么东东头都看晕了:)
 
同意 WilliamGui ,唯一的问题是数据量不能上万

否则,自己写,主要是效率问题
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部