Recent

Author Topic: [SOLVED] Update DBGrid row  (Read 4581 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
[SOLVED] Update DBGrid row
« 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.
« Last Edit: March 10, 2021, 11:47:56 am by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Update DBGrid row
« Reply #1 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.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Update DBGrid row
« Reply #2 on: March 09, 2021, 06:44:36 am »
Thanks. Any code?
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Update DBGrid row
« Reply #3 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

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Update DBGrid row
« Reply #4 on: March 09, 2021, 07:22:19 am »
Thanks, i'll have a look. BTW I use zeos.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

EgonHugeist

  • Jr. Member
  • **
  • Posts: 78
Re: Update DBGrid row
« Reply #5 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.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Update DBGrid row
« Reply #6 on: March 10, 2021, 02:23:40 am »
Any sample of how to it?
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Update DBGrid row
« Reply #7 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;
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

 

TinyPortal © 2005-2018