张
张鸿林
Unregistered / Unconfirmed
GUEST, unregistred user!
a table
==============
row_column Col_column value
a 1 v1
a 2 v2
b 1 v3
b 2 v4
key字段 为 row_column和 Col_column
想把它分析到grid中,像这样:
1 2
=======================
a v1 v2
b v3 v4
现有我的算法
扫描row_column,找出唯一,扫描Col_column ,找出唯一
扫描value,填到表中
效率不高,谁提供更好的算法
procedure LoadData(CDS:TClientDataSet;aStringGrid:TStringGrid;CrossTabDesc,RowField,ColField,ValueField,StatusField:String;
var aStatus:TSelfStatus);
var
i,iRow,jCol:Integer;
RowName,ColName,Status:String;
RowString,ColString:TStrings;
// origColor:TColor;
// Rect:TRect;
begin
RowString:=TStringList.Create;
ColString:=TStringList.Create;
try
//扫描行
CDS.First;
while not cds.Eofdo
begin
if RowString.IndexOf(CDS.FieldByName(RowField).AsString)=-1 then
RowString.Add(CDS.FieldByName(RowField).AsString);
CDS.Next;
end;
//扫描列
CDS.First;
while not cds.Eofdo
begin
if ColString.IndexOf(CDS.FieldByName(ColField).AsString)=-1 then
ColString.Add(CDS.FieldByName(ColField).AsString);
CDS.Next;
end;
with aStringGriddo
begin
//填充单元格
RowCount:=RowString.Count+1;
ColCount:=ColString.Count+1;
for jCol:=0 to ColCount-1do
for iRow:=0 to RowCount-1do
Cells[jCol,iRow]:='';
SetLength(aStatus,ColCount,RowCount);
if (RowCount<=1) then
FixedRows:=0
else
FixedRows:=1;
if (ColCount<=1)then
FixedCols:=0
else
FixedCols:=1;
Cells[0,0] := CrossTabDesc;
//填充行
for i:=0 to RowString.Count-1do
Cells[0,i+1]:=RowString;
//填充列
for i:=0 to ColString.Count-1do
Cells[i+1,0]:=ColString;
//填充值
CDS.First;
for i:=0 to CDS.RecordCount-1do
begin
RowName:=CDS.fieldbyname(RowField).AsString;
ColName:=CDS.fieldbyname(ColField).AsString;
Status:=CDS.fieldbyname(StatusField).AsString;
for iRow:=0 to RowCount-1do
if Cells[0,iRow]=RowName then
break;
for jCol:=0 to ColCount-1do
if Cells[jCol,0]=ColName then
break;
aStatus[jCol,iRow]:=Status;
Cells[jCol,iRow]:=CDS.FieldByName(ValueField).AsString;
CDS.Next;
end;
end;
finally
RowString.Free;
ColString.Free;
end;
end;
==============
row_column Col_column value
a 1 v1
a 2 v2
b 1 v3
b 2 v4
key字段 为 row_column和 Col_column
想把它分析到grid中,像这样:
1 2
=======================
a v1 v2
b v3 v4
现有我的算法
扫描row_column,找出唯一,扫描Col_column ,找出唯一
扫描value,填到表中
效率不高,谁提供更好的算法
procedure LoadData(CDS:TClientDataSet;aStringGrid:TStringGrid;CrossTabDesc,RowField,ColField,ValueField,StatusField:String;
var aStatus:TSelfStatus);
var
i,iRow,jCol:Integer;
RowName,ColName,Status:String;
RowString,ColString:TStrings;
// origColor:TColor;
// Rect:TRect;
begin
RowString:=TStringList.Create;
ColString:=TStringList.Create;
try
//扫描行
CDS.First;
while not cds.Eofdo
begin
if RowString.IndexOf(CDS.FieldByName(RowField).AsString)=-1 then
RowString.Add(CDS.FieldByName(RowField).AsString);
CDS.Next;
end;
//扫描列
CDS.First;
while not cds.Eofdo
begin
if ColString.IndexOf(CDS.FieldByName(ColField).AsString)=-1 then
ColString.Add(CDS.FieldByName(ColField).AsString);
CDS.Next;
end;
with aStringGriddo
begin
//填充单元格
RowCount:=RowString.Count+1;
ColCount:=ColString.Count+1;
for jCol:=0 to ColCount-1do
for iRow:=0 to RowCount-1do
Cells[jCol,iRow]:='';
SetLength(aStatus,ColCount,RowCount);
if (RowCount<=1) then
FixedRows:=0
else
FixedRows:=1;
if (ColCount<=1)then
FixedCols:=0
else
FixedCols:=1;
Cells[0,0] := CrossTabDesc;
//填充行
for i:=0 to RowString.Count-1do
Cells[0,i+1]:=RowString;
//填充列
for i:=0 to ColString.Count-1do
Cells[i+1,0]:=ColString;
//填充值
CDS.First;
for i:=0 to CDS.RecordCount-1do
begin
RowName:=CDS.fieldbyname(RowField).AsString;
ColName:=CDS.fieldbyname(ColField).AsString;
Status:=CDS.fieldbyname(StatusField).AsString;
for iRow:=0 to RowCount-1do
if Cells[0,iRow]=RowName then
break;
for jCol:=0 to ColCount-1do
if Cells[jCol,0]=ColName then
break;
aStatus[jCol,iRow]:=Status;
Cells[jCol,iRow]:=CDS.FieldByName(ValueField).AsString;
CDS.Next;
end;
end;
finally
RowString.Free;
ColString.Free;
end;
end;