Hello guys I have the following problem.
Suppose I have the following query
select field1, field2 from table;
and for my own personal reasons I need to have in DBGrid also field3 (which does not exist in the table), but I need to do my calculations.
How can I do at run time?! And possibly after I've already opened the query?!
I'm creating a special unit at the moment is this:
unit Unit_CampoComboDBGrid;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, db, Grids;
function AddDBComboBoxToGrid(Field:TField;NomeNuovoCampo: string;DataSet: TDataSet; CampoDataSet: string ;DataSetList: TDataSet; ChiaveLista: string; CampoLista: string): boolean;
implementation
function AddDBComboBoxToGrid(Field:TField;NomeNuovoCampo: string;DataSet: TDataSet; CampoDataSet: string ;DataSetList: TDataSet; ChiaveLista: string; CampoLista: string): boolean;
var
i: integer;
ret: boolean;
begin
ret:=false;
try
try
DataSet.Active:=False;
for i:=0 to DataSet.FieldDefs.Count-1 do
Field:=DataSet.FieldDefs[i].CreateField(DataSet);
Field:=TStringField.Create(DataSet);
with Field do
begin
FieldName:=NomeNuovoCampo;
Dataset:=DataSet;
FieldKind := fkLookup; //QUI DICO CHE E' UNA COMBOBOX
LookupDataSet:=DataSetList; //QUI ASSEGNO IL DATASET DA CUI EREDITARE I DATI DA FAR VEDERE NELLA COMBOBOX
LookupCache:=FALSE;
LookupKeyFields:=ChiaveLista;//'CHIAVE'; //CHIAVE CHE VERRA' INSERITA NEL CAMPO Temf.KeyFields
LookupResultField:=CampoLista;//'NOME'; //VALORE CHE VEDI NELLA COMBOBOX DA SELEZIONE
ReadOnly:=FALSE; //SOLA LETTURA
ProviderFlags:=[pfInUpdate, pfInWhere];
Required:=FALSE;
KeyFields:=CampoDataSet;//'CHIAVEPV';
end;
DataSet.Active:=True;
ret:=TRUE;
finally
end;
except
on E: Exception do
begin
end;
end;
result:=ret;
end;
end.
At the moment it does not work, but if I put this code in the form after I open the query and then not going for a function then magically it all works. Probably something to revise the parameters of the function.
Where am I wrong?
I want to understand how to solve through my unit in order to reuse the code.