本例效果图:


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

posted on 2008-03-20 11:30  万一  阅读(9987)  评论(43编辑  收藏  举报