这是我的源码,你自己看着改一下就可以的!
procedure CreatedxGridColumn(dxGrid: TdxDBGrid);
var
I,J,K: integer;
C: TdxDBTreeListColumnClass;
lFieldInfo:TFieldDictInfo;
daqLookup:TQuery;
D:TdxDBGridPickColumn;
sLookField,sSQL:String;
lSQLParser:TSQLAnalysis;
function GetListColumnsClass(AField: TField;DataSet:TDataSet): TdxDBTreeListColumnClass;
var
lFieldInfo:TFieldDictInfo;
lSQLParser:TSQLAnalysis;
begin
lSQLParser:=TSQLAnalysis.Create;
lSQLParser.SQLString:=(DataSet as TQuery).SQL.Text;
try
if AField.FieldKind = fkLookup then
Result := TdxDBGridLookupColumn
else begin
if AField.FieldKind = fkData then begin
case AField.DataType of
ftBoolean : Result := TdxDBGridCheckColumn;
ftDate, ftDateTime : Result := TdxDBGridDateColumn;
ftTime : Result := TdxDBGridTimeColumn;
ftCurrency : Result := TdxDBGridCurrencyColumn;
ftGraphic : Result := TdxDBGridImageColumn;
ftBytes, ftVarBytes, ftBlob, ftMemo,
ftFmtMemo, ftParadoxOle, ftDBaseOle, ftTypedBinary :
Result := TdxDBGridBlobColumn;
else
lFieldInfo := DBDictInfo.FindFieldDictInfo(AField.FieldName,lSQLParser.Froms);
if (lFieldInfo.LookupTable <> '') and (not AField.ReadOnly) then Result := TdxDBGridPickColumn
else Result := TdxDBGridColumn;
end;
end else
Result := TdxDBGridMaskColumn;
end;
finally
lSQLParser.Free;
end;
end;
begin
if dxGrid.DataSource = nil then Exit;
if dxGrid.DataSource.DataSet.FieldCount < 1 then exit;
if dxGrid.KeyField = '' then
dxGrid.KeyField := dxGrid.DataSource.DataSet.Fields[0].FieldName;
for I := 0 to dxGrid.DataSource.DataSet.FieldCount - 1 do
begin
if not dxGrid.DataSource.DataSet.Fields.Visible then Continue;
//显示MEMO字段,Modified by Xufeng
// if (GetListColumnsClass(dxGrid.DataSource.DataSet.Fields,dxGrid.DataSource.DataSet) = TdxDBGridBlobColumn) then Continue;
C := TdxDBTreeListColumnClass(GetListColumnsClass(dxGrid.DataSource.DataSet.Fields,dxGrid.DataSource.DataSet));
if C <> nil then
begin
with dxGrid.CreateColumn(C) do
begin
FieldName := dxGrid.DataSource.DataSet.Fields.FieldName;
//判断是否从另一个表里取数据
if C = TdxDBGridPickColumn then
begin
D := TdxDBGridPickColumn(dxGrid.ColumnByFieldName(dxGrid.DataSource.DataSet.Fields.FieldName));
lSQLParser:=TSQLAnalysis.Create;
lSQLParser.SQLString:=(dxGrid.DataSource.DataSet as TQuery).SQL.Text;
try
lFieldInfo := DBDictInfo.FindFieldDictInfo(dxGrid.DataSource.DataSet.Fields.FieldName,lSQLParser.Froms);
if lFieldInfo.LookupTable <> '' then
begin
daqLookup := TQuery.Create(nil);
try
K := Pos(';',lFieldInfo.LookupFieldsString);
if K=0 then sLookField := lFieldInfo.LookupFieldsString
else sLookField := Copy(lFieldInfo.LookupFieldsString,1,K-1);
sSQL := 'Select ' + sLookField + ' from ' + lFieldInfo.LookupTable;
if lFieldInfo.RangeField <> '' then sSQL := sSQL + ' where ' + lFieldInfo.RangeField + '=''' + lFieldInfo.RangeValue + '''';
daqLookup.SQL.Add(sSQL);
daqLookup.DatabaseName := GlobalInfos.DataBaseName;
daqLookup.Open;
for J := 1 to daqLookup.RecordCount do begin
D.Items.Add(daqLookup.FieldByName(sLookField).AsString);
daqLookup.Next;
end;
finally
daqLookup.Free;
end;
end;
finally
lSQLParser.Free;
end;
end;
end;
end;
end;
end;