Lazarus

Programming => Databases => Topic started by: pcurtis on March 08, 2021, 08:04:26 am

Title: [SOLVED] Update DBGrid row
Post by: pcurtis on March 08, 2021, 08:04:26 am
Hi All,
is it possible to update / refresh / reload a single row / record in a DBGrid without refreshing the entire dataset?

Thanks in advance.
Title: Re: Update DBGrid row
Post by: egsuh on March 09, 2021, 02:31:20 am
If you open separate query, and replace the row of dataset which is showing on the DBgrid, then it will be possible.
Title: Re: Update DBGrid row
Post by: pcurtis on March 09, 2021, 06:44:36 am
Thanks. Any code?
Title: Re: Update DBGrid row
Post by: egsuh on March 09, 2021, 07:14:02 am
You should have followings, and they should be hooked as follow to display the content of qr1 on DBGrid.

Code: Pascal  [Select][+][-]
  1.     qr1, qr2: TSQLQuery;
  2.     DBGrid: TDBGrid;
  3.     DataSource: TDataSource;
  4.  
  5.     DBGrid.DataSource= DataSource
  6.     DataSource.DataSet=qr1
  7.  
  8.     qr1.SQL.Text := 'select pk, ... from table1 where ....';
  9.  

Then you want to retrieve the record of current pk in qr2. That will look like:

Code: Pascal  [Select][+][-]
  1. var
  2.     field: TField;
  3. begin
  4.     qr2.SQL.Text:= 'select   .. from table1 where pk = ' + qr1.FieldByName('pk').AsString;
  5.     qr2.Open;
  6.  
  7.     qr1.Edit;
  8.     for field in qr2.Fields do
  9.          qr1.FieldByName(field.FieldName).AsString := field.AsString;
  10.     qr1.Post;
  11. end;

This should work theoretically, but not sure. I haven't tested whether TSQLQuery can be edited and posted  :D
Title: Re: Update DBGrid row
Post by: pcurtis on March 09, 2021, 07:22:19 am
Thanks, i'll have a look. BTW I use zeos.
Title: Re: Update DBGrid row
Post by: EgonHugeist on March 09, 2021, 06:44:25 pm
Zeos8 allows you to use methode RefreshCurrentRow by default. The method will fail if you use a "complex query" i.e joined columns. To cirumvent the exception use the TZUpdateSQL component for the TZDataSet component, assign a RefreshSQL query and execute RefreshCurrentRow.
Title: Re: Update DBGrid row
Post by: pcurtis on March 10, 2021, 02:23:40 am
Any sample of how to it?
Title: Re: Update DBGrid row
Post by: pcurtis on March 10, 2021, 11:14:07 am
OK, I dont understand TZUpdateQuery so I did it this way

  ZQuery1.DisableControls;
  ZQuery1.Locate - record to change
  ZQuery1.Edit;
  ZQuery1.Fields.Fields[xxx].AsWhatever := new value;
  ZQuery1.Post;
  ZQuery1.EnableControls;
TinyPortal © 2005-2018