根据查询生成表格
CreateColumns(dbbtvResult, 'SELECT * FROM ' + sViewName + ' where 1 = 0 ');
sSql := 'SELECT * FROM ' + sViewName + ' where 1 = 0 ';
function TfrmBatchSignature.CreateColumns(
AcxGrid1DBBandedTableView: TcxGridDBBandedTableView;
AsSql: string): string;
function IsAddBands(str: string): Boolean;//是否需要插入bans
var
J: Integer;
begin
Result := True;
for J := 0 to AcxGrid1DBBandedTableView.Bands.Count - 1 do
if AcxGrid1DBBandedTableView.Bands[j].Caption = str then
Result := False;
end;
function FieldTypeUpperCase(AField: TField):string;
begin
Result := FieldTypeNames[AField.DataType];
Result := UpperCase(Result);
//(tdttypeStr=UpperCase('Float'))
end;
var
I, J: integer;
BandsStr, CaptionStr, strSQL, sFieldType: string;
cxGridBand: TcxGridBand;
cxGridDBBandedColumn: TcxGridDBBandedColumn;
FuncOnGridTableDataCell: TMethod;
begin
//先清空表格数据
AcxGrid1DBBandedTableView.Bands.Clear;
with AcxGrid1DBBandedTableView do
begin
(DataController as IcxCustomGridDataController).DeleteAllItems;
(DataController as IcxCustomGridDataController).CreateAllItems(True);
end; //根据 datasource自动产生columns
Result := '';
if Trim(AsSql) = '' then Exit;
try
if not CommonDB.QryOpen(qryTemp, AsSql) then Exit;
// CommonDB.ExecQuery(qryTemp, AsSql);
qryTemp.DisableControls;
for i := 0 to qryTemp.FieldCount - 1 do
begin
sFieldType := FieldTypeUpperCase(qryTemp.Fields[i]);
if Pos('|', qryTemp.Fields[i].FieldName) > 0 then
begin
BandsStr := Copy(qryTemp.Fields[i].FieldName, 1, Pos('|', qryTemp.Fields[i].FieldName) - 1);
CaptionStr := Copy(qryTemp.Fields[i].FieldName, Pos('|', qryTemp.Fields[i].FieldName) + 1, 2555);
if AcxGrid1DBBandedTableView.Bands.Count = 0 then
AcxGrid1DBBandedTableView.Bands.Add.Caption := BandsStr;
if IsAddBands(BandsStr) then
AcxGrid1DBBandedTableView.Bands.Add.Caption := BandsStr;
cxGridDBBandedColumn := AcxGrid1DBBandedTableView.CreateColumn;
for J := 0 to AcxGrid1DBBandedTableView.Bands.Count - 1 do
begin
if AcxGrid1DBBandedTableView.Bands[j].Caption = BandsStr then
begin
cxGridDBBandedColumn.Caption := CaptionStr;
cxGridDBBandedColumn.DataBinding.FieldName := qryTemp.Fields[i].FieldName;
cxGridDBBandedColumn.Position.BandIndex := AcxGrid1DBBandedTableView.Bands[j].Position.ColIndex;
cxGridDBBandedColumn.HeaderAlignmentHorz := taCenter;
cxGridDBBandedColumn.HeaderAlignmentVert := vaCenter;
cxGridDBBandedColumn.Width := AcxGrid1DBBandedTableView.ViewInfo.Canvas.TextWidth(qryTemp.Fields[i].FieldName) + 10;
if (sFieldType = 'FLOAT') or (sFieldType = 'BCD') then
begin
cxGridDBBandedColumn.Options.Editing := False;
cxGridDBBandedColumn.PropertiesClass := TcxCalcEditProperties;
end;
end;
if (j = 0) and (AcxGrid1DBBandedTableView.Bands[j].Caption = '固定列') then
begin
AcxGrid1DBBandedTableView.Bands[j].FixedKind := fkLeft;
end;
end;
end
else
begin
cxGridBand := AcxGrid1DBBandedTableView.Bands.Add;
cxGridBand.Caption := '';
CaptionStr := qryTemp.Fields[i].FieldName;
cxGridDBBandedColumn := AcxGrid1DBBandedTableView.CreateColumn;
cxGridDBBandedColumn.Caption := CaptionStr;
cxGridDBBandedColumn.DataBinding.FieldName := qryTemp.Fields[i].FieldName;
cxGridDBBandedColumn.Position.BandIndex := AcxGrid1DBBandedTableView.Bands[cxGridBand.Index].Position.ColIndex;
cxGridDBBandedColumn.HeaderAlignmentHorz := taCenter;
cxGridDBBandedColumn.HeaderAlignmentVert := vaCenter;
cxGridDBBandedColumn.Width := AcxGrid1DBBandedTableView.ViewInfo.Canvas.TextWidth(qryTemp.Fields[i].FieldName) + 10;
if (sFieldType = 'FLOAT') or (sFieldType = 'BCD') then
begin
cxGridDBBandedColumn.Options.Editing := False;
cxGridDBBandedColumn.PropertiesClass := TcxCalcEditProperties;
end;
end;
end;
finally
qryTemp.EnableControls;
end;
end;