uses
dxDBGrid, dxDBCtrl, dxBar, dxEdLib, dxGrClms;
// 说明: 我的 dxDBGrid.DataSource.DataSet 是已经知道各个字段的类型的;具体的
// 类型你需要自己去修改;
procedure SetDBGrid(DBGrid : TdxDBGrid); // 根据dxDBGrid连接的 TDataSource 内容来动态建立 Column
var
I : Integer;
Column : TdxDBTreeListColumn;
ColumnClass : TdxDBTreeListColumnClass;
begin
with DBGrid do
begin
BeginUpdate;
BeginGrouping;
try
DefaultFields := False;
if Assigned(DataSource) and Assigned(DataSource.DataSet) then
begin
For I := 0 to DataSource.DataSet.FieldCount - 1 do
begin
if (I = DataSource.DataSet.FieldCount - 4) then
ColumnClass := TdxDBTreeListColumnClass(TdxDBGridCurrencyColumn) // 金额类型
else
ColumnClass := TdxDBTreeListColumnClass(TdxDBGridColumn); // 一般类型
if ColumnClass <> Nil then
begin
Column := CreateColumnEx(ColumnClass, DBGrid);
Column.FieldName := DataSource.DataSet.Fields.FieldName;
Column.HeaderAlignment := taCenter;
if I = 1 then
begin
Column.Width := 80;
Column.Alignment := taCenter;
Column.SummaryFooterField := Column.FieldName;
Column.SummaryFooterType := cstCount;
Column.SummaryFooterFormat := '0';
end
else if I = 2 then
begin
Column.Width := 45;
.
.
.
end;
end;
end;
finally
EndGrouping;
EndUpdate;
end;
end;
end;