Recent

Author Topic: Add the field to the dataset  (Read 8885 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2092
    • Lazarus and Free Pascal italian community
Add the field to the dataset
« on: January 31, 2014, 12:13:10 pm »
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:

Code: [Select]
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.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

mangakissa

  • Hero Member
  • *****
  • Posts: 1090
Re: Add the field to the dataset
« Reply #1 on: January 31, 2014, 01:34:10 pm »
- go to your T()Query component.
- Open the fieldeditor with right mouse click -> edit fields
- select  "add fields" from fielddefs
- select all your fields and go back to the fieldeditor
- create "new field".
- select calculated as fieldtype and fill in the required fields
- close form "edit fields"

with the property OnCalcfields you can fill your extra field.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: Add the field to the dataset
« Reply #2 on: January 31, 2014, 02:01:56 pm »
No, the question is: "How can I do at run time?!". But I agree,  mangakissa,it's ok to create field before, at design time and use it in run time.
« Last Edit: January 31, 2014, 02:05:22 pm by exdatis »

mangakissa

  • Hero Member
  • *****
  • Posts: 1090
Re: Add the field to the dataset
« Reply #3 on: January 31, 2014, 08:04:50 pm »
found this option with google:
Code: [Select]
Procedure AddCalcStringField(Table1: tDataset; fName,fDisplay:String;
Size:
Integer);
{ Adds a calculated tStringField to a table at runtime}
var
f : TField;
i : integer;
begin
Table1.FieldDefs.Update;
Table1.Close;
for i := 0 to Table1.FieldDefs.Count - 1 do
if table1.FindField(table1.FieldDefs[i].Name) = nil then
table1.FieldDefs.Items[i].CreateField(Table1);

if table1.FindField(fName) <> nil then
Exit;

f := tStringField.Create(Table1);
f.Name := Table1.Name+fName;
f.FieldName := fName;
f.DisplayLabel := fDisplay;
f.Calculated := True;
f.DataSet := Table1;
Table1.Open;
end;
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2092
    • Lazarus and Free Pascal italian community
Re: Add the field to the dataset
« Reply #4 on: February 01, 2014, 03:21:25 pm »
I tried the code and it works, but I need it to be a combobox. Then I tried to modify the code so? But the following line

FieldKind := fkLookup; // <--ERROR

sends the loop my program. Why?

Code: [Select]
Procedure AddCalcStringField(fName: string;fDisplay:String;Size:Integer;Table1: tDataset; CampoDataSet: string; DataSetList: TDataSet; ChiaveLista: string; CampoLista: string);
{ Adds a calculated tStringField to a table at runtime}
var
f : TField;
i : integer;
begin
Table1.FieldDefs.Update;
Table1.Close;
for i := 0 to Table1.FieldDefs.Count - 1 do
if table1.FindField(table1.FieldDefs[i].Name) = nil then
table1.FieldDefs.Items[i].CreateField(Table1);

if table1.FindField(fName) <> nil then
Exit;

f := tStringField.Create(Table1);
with f do
begin
  FieldName := Table1.Name+fName;
  Name := fName;
  DisplayLabel := fDisplay;
  Calculated := True;
  DataSet := Table1;

  FieldKind := fkLookup; // <--ERROR

  LookupDataSet:=DataSetList;
  LookupCache:=FALSE;
  LookupKeyFields:=ChiaveLista;
  LookupResultField:=CampoLista;

  ReadOnly:=FALSE;
  ProviderFlags:=[pfInUpdate, pfInWhere];
  Required:=FALSE;
  KeyFields:=CampoDataSet;

end;
Table1.Open;
end;
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

mangakissa

  • Hero Member
  • *****
  • Posts: 1090
Re: Add the field to the dataset
« Reply #5 on: February 01, 2014, 10:05:48 pm »
fkLookup doesn't work on most  dataquery components.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2092
    • Lazarus and Free Pascal italian community
Re: Add the field to the dataset
« Reply #6 on: February 03, 2014, 01:44:34 pm »
But this works.

I did a test example. Someone give me a hand to figure out how I can remedy the problem?

You must have installed Zeos only as a package.
Unzip and compiled. Then he crushed button1 you will see that it works, but if you press on button 2 you will see that nothing changes.

In addition, if you try a button then restart the program and try the other because otherwise it goes in error.

thank you very much
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2092
    • Lazarus and Free Pascal italian community
Re: Add the field to the dataset
« Reply #7 on: February 05, 2014, 06:24:01 pm »
No idea? Am I wrong or is that a bug?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018