学习 TTreeView [15] - 连接数据库 (作为给 "丁永其" 和 "DELPHI万岁" 两位朋友的回复)

本例效果图:

学习 TTreeView [15] - 连接数据库 (作为给 "丁永其" 和 "DELPHI万岁" 两位朋友的回复)
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, DB, DBClient, StdCtrls, ComCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    DBGrid1: TDBGrid;
    TreeView1: TTreeView;
    Splitter1: TSplitter;
    procedure FormCreate(Sender: TObject);
    procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
  node: TTreeNode;
  List: TStringList;
  s: string;
begin
  {country.xml 是 Delphi 的数据示范文件, 也可以用 country.cds}
  ClientDataSet1.FileName := 'c:\temp\country.xml';
  ClientDataSet1.Active := True;
  DataSource1.DataSet := ClientDataSet1;
  DBGrid1.DataSource := DataSource1;
  {上面四行是让数据构件互相挂接, 也可以在设计时设定}

  {建立 List}
  List := TStringList.Create;
  List.Sorted := True;           {指定排序}
  List.Duplicates := dupIgnore;  {避免重复}

  {把数据加入到 TreeView}
  for i := 0 to ClientDataSet1.FieldDefs.Count - 1 do
  begin
    node := TreeView1.Items.Add(nil, ClientDataSet1.FieldDefs[i].Name); {字段名}

    {为避免数据重复, 先把数据给 List}
    List.Clear;
    ClientDataSet1.First;
    while not ClientDataSet1.Eof do
    begin
      List.Add(ClientDataSet1.Fields[i].value);
      ClientDataSet1.Next;
    end;
    {把 List 中的数据加入到 TreeView}
    for s in List do TreeView1.Items.AddChild(node, s);
  end;

  List.Free;
end;

procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
  if Node.Parent <> nil then
  begin
    {下面的 Filter 其实就是 SQL 查询语句, 如果用其他数据库替换就是}
    ClientDataSet1.Filter := Node.Parent.Text + '=''' + Node.Text + '''';
    ClientDataSet1.Filtered := True;
  end else ClientDataSet1.Filtered := False; {选字段名时取消过虑}
end;

end.

 
 
 
 
 

 

 
  
附数据库样本: &lt;?xml version=&quot;1.0&quot; standalone=&quot;yes&quot;?&gt; &lt;DATAPACKET Version=&quot;2.0&quot;&gt; &lt;METADATA&gt; &lt;FIELDS&gt; &lt;FIELD attrname=&quot;Name&quot; fieldtype=&quot;string&quot; WIDTH=&quot;24&quot;/&gt; &lt;FIELD attrname=&quot;Capital&quot; fieldtype=&quot;string&quot; WIDTH=&quot;24&quot;/&gt; &lt;FIELD attrname=&quot;Continent&quot; fieldtype=&quot;string&quot; WIDTH=&quot;24&quot;/&gt; &lt;FIELD attrname=&quot;Area&quot; fieldtype=&quot;r8&quot;/&gt; &lt;FIELD attrname=&quot;Population&quot; fieldtype=&quot;r8&quot;/&gt; &lt;/FIELDS&gt; &lt;PARAMS DEFAULT_ORDER=&quot;1&quot; PRIMARY_KEY=&quot;1&quot; LCID=&quot;2057&quot;/&gt; &lt;/METADATA&gt; &lt;ROWDATA&gt; &lt;ROW Name=&quot;Argentina&quot; Capital=&quot;Buenos Aires&quot; Continent=&quot;South America&quot; Area=&quot;2777815&quot; Population=&quot;32300003&quot;/&gt; &lt;ROW Name=&quot;Bolivia&quot; Capital=&quot;La Paz&quot; Continent=&quot;South America&quot; Area=&quot;1098575&quot; Population=&quot;7300000&quot;/&gt; &lt;ROW Name=&quot;Brazil&quot; Capital=&quot;Brasilia&quot; Continent=&quot;South America&quot; Area=&quot;8511196&quot; Population=&quot;150400000&quot;/&gt; &lt;ROW Name=&quot;Canada&quot; Capital=&quot;Ottawa&quot; Continent=&quot;North America&quot; Area=&quot;9976147&quot; Population=&quot;26500000&quot;/&gt; &lt;ROW Name=&quot;Chile&quot; Capital=&quot;Santiago&quot; Continent=&quot;South America&quot; Area=&quot;756943&quot; Population=&quot;13200000&quot;/&gt; &lt;ROW Name=&quot;Colombia&quot; Capital=&quot;Bagota&quot; Continent=&quot;South America&quot; Area=&quot;1138907&quot; Population=&quot;33000000&quot;/&gt; &lt;ROW Name=&quot;Cuba&quot; Capital=&quot;Havana&quot; Continent=&quot;North America&quot; Area=&quot;114524&quot; Population=&quot;10600000&quot;/&gt; &lt;ROW Name=&quot;Ecuador&quot; Capital=&quot;Quito&quot; Continent=&quot;South America&quot; Area=&quot;455502&quot; Population=&quot;10600000&quot;/&gt; &lt;ROW Name=&quot;El Salvador&quot; Capital=&quot;San Salvador&quot; Continent=&quot;North America&quot; Area=&quot;20865&quot; Population=&quot;5300000&quot;/&gt; &lt;ROW Name=&quot;Guyana&quot; Capital=&quot;Georgetown&quot; Continent=&quot;South America&quot; Area=&quot;214969&quot; Population=&quot;800000&quot;/&gt; &lt;ROW Name=&quot;Jamaica&quot; Capital=&quot;Kingston&quot; Continent=&quot;North America&quot; Area=&quot;11424&quot; Population=&quot;2500000&quot;/&gt; &lt;ROW Name=&quot;Mexico&quot; Capital=&quot;Mexico City&quot; Continent=&quot;North America&quot; Area=&quot;1967180&quot; Population=&quot;88600000&quot;/&gt; &lt;ROW Name=&quot;Nicaragua&quot; Capital=&quot;Managua&quot; Continent=&quot;North America&quot; Area=&quot;139000&quot; Population=&quot;3900000&quot;/&gt; &lt;ROW Name=&quot;Paraguay&quot; Capital=&quot;Asuncion&quot; Continent=&quot;South America&quot; Area=&quot;406576&quot; Population=&quot;4660000&quot;/&gt; &lt;ROW Name=&quot;Peru&quot; Capital=&quot;Lima&quot; Continent=&quot;South America&quot; Area=&quot;1285215&quot; Population=&quot;21600000&quot;/&gt; &lt;ROW Name=&quot;United States of America&quot; Capital=&quot;Washington&quot; Continent=&quot;North America&quot; Area=&quot;9363130&quot; Population=&quot;249200000&quot;/&gt; &lt;ROW Name=&quot;Uruguay&quot; Capital=&quot;Montevideo&quot; Continent=&quot;South America&quot; Area=&quot;176140&quot; Population=&quot;3002000&quot;/&gt; &lt;ROW Name=&quot;Venezuela&quot; Capital=&quot;Caracas&quot; Continent=&quot;South America&quot; Area=&quot;912047&quot; Population=&quot;19700000&quot;/&gt; &lt;/ROWDATA&gt; &lt;/DATAPACKET&gt;

你可能感兴趣的:(学习 TTreeView [15] - 连接数据库 (作为给 "丁永其" 和 "DELPHI万岁" 两位朋友的回复))