熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Delphi編程 >> 正文

Delphi學堂之在Delphi中自己建立交叉表

2013-11-11 21:04:31  來源: Delphi編程 
function CreateTmptab(const AFieldDefs:TFieldDefs):TDataSet;
  var
  TempTable:TatClientDataSet;
  begin
  TempTable:=nil;
  Result:=nil;
  if AFieldDefs<>nil then
  begin
    try
      TempTable:=TatClientDataSetCreate(Application);
      TempTableFieldDefsAssign(AFieldDefs);
      TempTableCreateDataSet;
      Result:=(TempTable as TDataSet);
    Except
    if TempTable<>nil then
      TempTableFree;
      Result:=nil;
      raise;
    end
  end;
  end;
  {
  SouDataset源數據集
  ColField交叉表動態列字段
  RowField交叉表行字段
  DataField數據字段
  }
  function GenCrossTable(SouDataset:tdataset;ColFieldRowFieldDataField:string):tdataset;
  var
  Vdataset:tdataset;
  tmpdataset:tatclientdataset;
  DataSource:tdatasource;
  tmpstrs:tstrings;
  rowvalcolvaldataval:string;
  ij:integer;
  datatype:TFieldType;
  DataSize:integer;
  begin
  result:=nil;
  if (ColField=) or(RowField=)or(DataField=) then
   showmessage(All Field not be NULL!)
  else
  begin
   if (ColField=RowField)
     or(ColField=DataField)
     or(RowField=DataField) then
    showmessage(All Field not be Equ!)
   else
   if (selfSouDataSetFieldByName(ColField)DataType=ftString)
    or (selfSouDataSetFieldByName(ColField)DataType<>ftWideString)
    or (selfSouDataSetFieldByName(ColField)DataType<>ftFixedChar)
    or (selfSouDataSetFieldByName(ColField)DataType<>ftMemo)
    or (selfSouDataSetFieldByName(ColField)DataType<>ftFmtMemo) then
   begin
   try
    tmpstrs:=tstringlistCreate;
    Vdataset:=SouDataSet;
    VdatasetFirst;
    for i:= to VdatasetRecordCount do
    begin
     if (varisnull(SouDataSetFieldValues[colfield])=false) and (SouDataSetFieldValues[colfield]<>) then
      if tmpstrsIndexOf(SouDataSetFieldValues[colfield])= then
      begin
       tmpstrsAdd(SouDataSetFieldValues[colfield]);
      end;
     VdatasetNext;
    end;
    //生成動態列標題
    tmpdataset:=TClientDataSetCreate(Self);
    tmpdatasetFieldDefsAdd(rowfieldftstringFalse);
    for i:= to tmpstrsCount do
    begin
     with tmpdatasetFieldDefs do
     begin
      Add(tmpstrsStrings[i]ftIntegerFalse);
     end;
    end;
    tmpdatasetFieldDefsAdd(SumftIntegerFalse);
    DataSource:=tdatasourceCreate(self);
    DataSourceDataSet:=tmpdataset;
    with DataSource do
    begin
     dataset:=Createtmptab(tmpdatasetFieldDefs);
     datasetOpen;
    end;
    //建立臨時表
    VdatasetFirst;
    for i:= to VdatasetRecordCount do
    begin
     rowval:=SouDataSetfieldbyname(rowfield)AsString;
     colval:=SouDataSetfieldbyname(colfield)AsString;
     dataval:=SouDataSetfieldbyname(datafield)AsString;
     if dataval= then dataval:=;
     if DataSourceDataSetLocate(rowfieldrowval[loPartialKey]) then
     begin
      DataSourceDataSetEdit;
      DataSourceDataSetFieldByName(colval)AsString:=dataval;
      DataSourceDataSetFieldByName(Sum)AsInteger:=
       DataSourceDataSetFieldByName(Sum)AsInteger+strtoint(dataval);
      DataSourceDataSetPost;
     end
     else
     begin
      DataSourceDataSetAppend;
      DataSourceDataSetFieldByName(rowfield)AsString:=rowval;
      for j:= to DataSourceDataSetFieldsCount do
       DataSourceDataSetFields[j]AsCurrency:=;
      DataSourceDataSetFieldByName(colval)AsString:=dataval;
      DataSourceDataSetFieldByName(Sum)AsString:=dataval;
      DataSourceDataSetPost;
     end;
     VdatasetNext;
    end;
    result:=DataSourceDataSet;
    //生成交叉表數據集
    tmpstrsFree;
   except
   end;
   end
   else
    showmessage(ColField Must be of Type String!) ;
  end;
  end;
  
  以上代碼在D和SQL Server /測試通過
From:http://tw.wingwit.com/Article/program/Delphi/201311/8419.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.