帮助里说得很清楚: The following code defines a record type of TMyRec and a record pointer type of PMyRec.type PMyRec = ^TMyRec; TMyRec = record FName: string; LName: string; end;Assuming these types are used, the following code adds a node to TreeView1 as the last sibling of a specified node. A TMyRec record is associated with the added item. The FName and LName fields are obtained from edit boxes Edit1 and Edit2. The Index parameter is obtained from edit box Edit3. The item is added only if the Index is a valid value.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;After an item containing a TMyRec record has been added, the following code retrieves the FName and LName values associated with the item and displays the values in a label.procedure TForm1.Button2Click(Sender: TObject);begin Label1.Caption := PMyRec(TreeView1.Selected.Data)^.FName + ' ' + PMyRec(TreeView1.Selected.Data)^.LName; end;
一般你不可能将一条记录的所有数据存放在TTreeNode中,一般也就是存放ID,Text,通过TTreeView的OnClick事件获取Selected的TTreeNode,获取存储在Data中的ID,根据ID从数据库中取记录。OnClick事件: var ssql : String; ID : Integer; TempNode: TTreeNode; begin TempNode := TTreeView1.Selected; If TempNode=nil Exit; //没有选择节点 ID := Integer(TempNode.Data); ssql := 'SELECT * FROM TableName WHERE ID='+IntToStr(ID); With AdoQuery1 do begin Close; SQL.Lines.Clear; SQL.Lines.Add(ssql); Open; //取记录的字段数据 If RecordCount>0 then begin XX := FieldByName('XXXX').AsString; end; end;如上
写了一个例子,测试成功 你可以看看: procedure TForm1.Button1Click(Sender: TObject); var APMyRec : array of PMyRec; iCount, sMaxSize : Integer; sTopNode : TTreeNode; begin if not ClientDataSet1.Active then ClientDataSet1.Open; sMaxSize := ClientDataSet1.RecordCount; SetLength(APMyRec,sMaxSize); ClientDataSet1.First; for iCount := 0 to sMaxSize - 1 do begin New(APMyRec[iCount]); APMyRec[iCount]^.sName := ClientDataSet1.FieldByName('xm').AsString; APMyRec[iCount]^.sSex := ClientDataSet1.FieldByName('xb').AsString; ClientDataSet1.Next; end; sTopNode := TreeView1.Items.Add(nil,'根结点'); for iCount := 0 to sMaxSize - 1 do begin TreeView1.Items.AddObject(sTopNode,APMyRec[iCount]^.sName,APMyRec[iCount]); end; end;procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode); begin if Node.Data <> nil then begin Edit1.Text := PMyRec(Node.data).sName; Edit2.Text := PMyRec(Node.data).sSex; end; end;
Select * From MyTable Where Description = 'XXXXXX';
在ONCHANGE事件中
if node.data<>nil then
self.label1.caption:=pmyData(node.data)^.Fname+pmyData(node.data)^.Lname
但是现在老取得是一个接点的信息呀
node : Ttreenode ;node := treeview1.Items.GetFirstNode ;
.... := node.text ;然后用node.next得到下一个节点
用node.GetFirstChild得到第一个子节点
用node.GetNextChild得到下一个子节点
然后重复GET,就可以了
得到的节点的TEXT就应该是你要的数据
The following code defines a record type of TMyRec and a record pointer type of PMyRec.type
PMyRec = ^TMyRec;
TMyRec = record
FName: string;
LName: string;
end;Assuming these types are used, the following code adds a node to TreeView1 as the last sibling of a specified node. A TMyRec record is associated with the added item. The FName and LName fields are obtained from edit boxes Edit1 and Edit2. The Index parameter is obtained from edit box Edit3. The item is added only if the Index is a valid value.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;After an item containing a TMyRec record has been added, the following code retrieves the FName and LName values associated with the item and displays the values in a label.procedure TForm1.Button2Click(Sender: TObject);begin
Label1.Caption := PMyRec(TreeView1.Selected.Data)^.FName + ' ' +
PMyRec(TreeView1.Selected.Data)^.LName;
end;
ScoutKing(失眠夜) :老兄,看清楚了,我取数据的代码和你的有什么分别呀
如果不行,请检查存储过程是否正确!
有没有问题
这个pmydata(TreeView1.Selected.Data)^.FName我试过了
这是我的代码帮忙看看
pn.name:=query1.fieldbyname('name').AsString;
pn.size:=query1.fieldbyname('size').asstring;
pn.wight:=query1.fieldbyname('weight').AsString;
treeview1.Items.AddObject(tn,pn.name,pn) ;
var
ssql : String;
ID : Integer;
TempNode: TTreeNode;
begin
TempNode := TTreeView1.Selected;
If TempNode=nil Exit; //没有选择节点
ID := Integer(TempNode.Data);
ssql := 'SELECT * FROM TableName WHERE ID='+IntToStr(ID);
With AdoQuery1 do begin
Close;
SQL.Lines.Clear;
SQL.Lines.Add(ssql);
Open;
//取记录的字段数据
If RecordCount>0 then begin
XX := FieldByName('XXXX').AsString;
end;
end;如上
你可以看看:
procedure TForm1.Button1Click(Sender: TObject);
var
APMyRec : array of PMyRec;
iCount,
sMaxSize : Integer;
sTopNode : TTreeNode;
begin
if not ClientDataSet1.Active then
ClientDataSet1.Open;
sMaxSize := ClientDataSet1.RecordCount;
SetLength(APMyRec,sMaxSize);
ClientDataSet1.First;
for iCount := 0 to sMaxSize - 1 do
begin
New(APMyRec[iCount]);
APMyRec[iCount]^.sName := ClientDataSet1.FieldByName('xm').AsString;
APMyRec[iCount]^.sSex := ClientDataSet1.FieldByName('xb').AsString;
ClientDataSet1.Next;
end; sTopNode := TreeView1.Items.Add(nil,'根结点');
for iCount := 0 to sMaxSize - 1 do
begin
TreeView1.Items.AddObject(sTopNode,APMyRec[iCount]^.sName,APMyRec[iCount]);
end;
end;procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
if Node.Data <> nil then
begin
Edit1.Text := PMyRec(Node.data).sName;
Edit2.Text := PMyRec(Node.data).sSex;
end;
end;