请熟悉dxDBGrid的看一下,怎样在运行时动态添加一列(column),100分相送!(100分)

  • 主题发起人 主题发起人 Dawn7
  • 开始时间 开始时间
D

Dawn7

Unregistered / Unconfirmed
GUEST, unregistred user!
我只能在设计时用dxDBGrid的设计器用Add All Fields添加字段列,在运行时怎么用?
有象dbGrid那样的吗?比如说直接设置某个属性就可以了,如不行,请给一段编程的代码
也可以!不胜感激!
 
各位大侠帮帮忙!急需的,我用CreateColumn方法不行,怎么办?
 
这是我的源码,你自己看着改一下就可以的!
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;
 
太感谢了!不知说什么好,我看了许久的帮助都没明白,嘿嘿,现在终于知道一些了,
我所要的已经实现了!谢谢!
以后多向你请教!
 
能不能先加进去,然后控制它的显示。
 
to:hpretty
我没有看明白是如何对pick类型字段的Item动态付值的 。

你的“D”变量是起这个作用吧?好像比较孤立,跟“C”没有什么关系。请大侠赐教。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部