其他语言

位置:IT落伍者 >> 其他语言 >> 浏览文章

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


发布日期:2020年11月17日
 
Delphi学堂之在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 /测试通过               

上一篇:用Delphi2005学设计模式之工厂方法篇

下一篇:Delphi学习使用资源文件-Ico