1、数据库来源是一张表;
2、这张表中有一个代码字段(层次码),如
   0100 0101 0102 0200 0201 0202
3、想在cxgrid中显示如下效果
代码     名称   
0100     代码1  
  0101   代码2
  0102   代码3
0200     代码4
  0201   代码5
  0202   代码6

各行数据可以像treeview控件的节点一样收缩和展开。

解决方案 »

  1.   

    一、建立一个例子数据表,三个字段:ID 自增类型;p_id 类型为 string,长度6;nn 类型 string 长度 6。
    二、新建一个工程、在窗体摆放:一个 ADOConnection1(并设置好连接串);两个TADOQuery:ADOQuery1、ADOQuery2;一个DataSetProvider1;一个ClientDataSet1;两个TDataSource:DataSource1、DataSource2 ,在 ADOQuery2 加入所有字段,并建立一个名为 js 的计算字段,类型为 string 长度 6(全部数据控件的连接关系已经写在代码中,无需设置)。
    三、双击窗体、将下列代码覆盖你的 Unit1 ,再关联 ADOQuery2 的 OnCalcFields 事件(事件代码已经写在下面,仅关联一下即可)。unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
      cxStyles, cxCustomData, cxFilter, cxData, cxDataStorage, cxEdit, DB,
      cxDBData, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
      ADODB, cxGridLevel, cxClasses, cxGridCustomView, cxGrid, Provider,
      DBClient;type
      TForm1 = class(TForm)
        DataSource1: TDataSource;
        ADOQuery2: TADOQuery;
        ADOQuery2id: TAutoIncField;
        ADOQuery2p_id: TWideStringField;
        ADOQuery2nn: TWideStringField;
        ADOQuery2js: TStringField;
        ADOQuery1: TADOQuery;
        ADOConnection1: TADOConnection;
        DataSource2: TDataSource;
        ClientDataSet1: TClientDataSet;
        DataSetProvider1: TDataSetProvider;
        procedure FormCreate(Sender: TObject);
        procedure ADOQuery2CalcFields(DataSet: TDataSet);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    uses MidasLib;//免发布 Midas.dll 文件var cxGrid1: TcxGrid;
        cxGrid1Level1: TcxGridLevel;
        cxGrid1Level2: TcxGridLevel;
        cxGrid1DBTableView1: TcxGridDBTableView;
        cxGrid1DBTableView2: TcxGridDBTableView;procedure TForm1.FormCreate(Sender: TObject);
    begin
      //数据控件设置:
      ADOConnection1.Close;
      ADOConnection1.Open;
      ADOQuery1.Connection:=ADOConnection1;
      ADOQuery1.SQL.Text:='select id,p_id,nn from abc where RIGHT(p_id,2) = '+QuotedStr('00');
      ADOQuery1.Open;
      DataSource1.DataSet:=ADOQuery1;
      ADOQuery2.Connection:=ADOConnection1;
      ADOQuery2.SQL.Text:='select id,p_id,nn from abc where RIGHT(p_id,2) <> '+QuotedStr('00');
      ADOQuery2.Open;
      DataSetProvider1.DataSet:=ADOQuery2;
      ClientDataSet1.ProviderName:='DataSetProvider1';
      ClientDataSet1.Open;
      DataSource2.DataSet:=ClientDataSet1;
      //主表格:
      cxGrid1:=TcxGrid.Create(self);
      cxGrid1.Parent:=self;
      cxGrid1.Align:=alTop;
      cxGrid1Level1:=cxGrid1.Levels.Add;
      cxGrid1Level1.Name:='cxGrid1Level1';
      cxGrid1DBTableView1:=cxGrid1.CreateView(TcxGridDBTableView) as TcxGridDBTableView;
      cxGrid1DBTableView1.Name:='cxGrid1DBTableView1';
      cxGrid1Level1.GridView:=cxGrid1DBTableView1;
      cxGrid1DBTableView1.DataController.DataSource:=DataSource1;
      cxGrid1DBTableView1.DataController.CreateAllItems;
      cxGrid1DBTableView1.Items[0].Free;
      cxGrid1DBTableView1.OptionsView.GroupByBox:=false;
      //从表格:
      cxGrid1Level2:=cxGrid1Level1.Add;
      cxGrid1Level2.Name:='cxGrid1Level2';
      cxGrid1DBTableView2:=cxGrid1.CreateView(TcxGridDBTableView) as TcxGridDBTableView;
      cxGrid1DBTableView2.Name:='cxGrid1DBTableView2';
      cxGrid1Level2.GridView:=cxGrid1DBTableView2;
      cxGrid1DBTableView2.DataController.DataSource:=DataSource2;
      cxGrid1DBTableView2.DataController.CreateAllItems;
      cxGrid1DBTableView2.Items[1].Free;
      cxGrid1DBTableView2.Items[0].Free;
      cxGrid1DBTableView2.OptionsView.GroupByBox:=false;
      cxGrid1DBTableView2.OptionsView.Header:=false;
      cxGrid1DBTableView2.DataController.DetailKeyFieldNames:='js';
      cxGrid1DBTableView2.DataController.KeyFieldNames:='id';
      cxGrid1DBTableView2.DataController.MasterKeyFieldNames:='p_id';
    end;procedure TForm1.ADOQuery2CalcFields(DataSet: TDataSet);// js 为计算字段 string 类型
    begin
      if copy(ADOQuery2.FieldByName('p_id').AsString,3,2)='00' then
        ADOQuery2.FieldByName('js').AsString:=
          ADOQuery2.FieldByName('p_id').AsString
      else
        ADOQuery2.FieldByName('js').AsString:=
          copy(ADOQuery2.FieldByName('p_id').AsString,1,2)+'00';
    end;end.
      

  2.   

    测试的记录按下列情况录入六笔:字段名:  p_id  nn  
             0100 代码1   
             0101 代码2
             0102 代码3
             0200 代码4
             0201 代码5
             0202 代码6