Pregunta

I may have been too clever for my own good :-/

I have a table which holds some pressure measurements. These are always stored as PSI, but the user can select a radio group button to toggle between PSI and BAR.

In order to keep the code clean and and push work onto the database, I created a second table for configuration items, with a single row. One column psi_bar_conversion will take the value either 1 or 14.5 as the user toggles the radio group.

In Delphi, my query which ties to my DB grid is set up with statements like

SELECT ROUND(inlet_waterPressure_psi  /  
                  (SELECT psi_bar_conversion FROM configuration), 
             (SELECT float_precision FROM configuration))
       AS inlet_waterPressure, 
FROM measurements

All of which works just fine (and perhaps I am explaining too much).

All that I am tring to do is add some code in the function which handles the radio button toggle to force my DB grid to refresh its contents becuase I have just updated the value of configuration.psi_bar_conversion (but no direct field of my query, nor of my datasource).

Should I invoke Refresh() or Invalidate() or SomeOtherFunction() - of the DB grid, the query, the datasrouce? That's what is confusing me.

Thanks in advance for any help ....

¿Fue útil?

Solución

You need to close and then reopen the query to have the change in psi_bar_conversion and float_precision to take effect. The two sub-selects (for the values from configuration) only happen when the query is executed.

Otros consejos

TDBGrid presentation depends on the connected TDataSet (via a TDataSource).

To update the Grid Values you have to refresh the data in TDataSet with the method TDataSet.Refresh.

To update a special Grid you can refresh the connected DataSet like this:

DBGrid1.DataSource.DataSet.Refresh;

But some TDataSet descendants will not refresh and that is documented by Embarcadero

TDataSet.Refresh

It depends on the components you use (i did a test with UniDAC and it works fine)

procedure TForm1.RadioGroup1Click( Sender : TObject );
var
  LRate : Extended;
begin
  case RadioGroup1.ItemIndex of
    0 :
      LRate := 1;
    1 :
      LRate := 14.5;
  end;
  UniConnection1.ExecSQL( 'UPDATE configuration SET psi_bar_conversion = :conversion', [LRate] );
  DBGrid1.DataSource.DataSet.Refresh;
end;

If your components did not refetch the data on calling Refresh, then you have to close and reopen (also stated by documentation)

DBGrid1.DataSource.DataSet.Close;
DBGrid1.DataSource.DataSet.Open;

IMHO such components are not fully implemented, so this is just a workaround having side effects in calling some maybe unwanted events (BeforeClose, AfterClose, BeforeOpen, AfterOpen) which will not get fired using Refresh.

But at all it has nothing to do with SubSelects.

I'm afarid I can't help you with the Delphi side of things here.

On the datbase side of things....

Is there any reason that you can't just store both the bar and psi values in the database?

You could do the conversion when saving and then you are left just to do a simple select on the data when you want to view it. This could be done by the software that is performing the save or by a trigger in the database.

The reason I suggest this is that the psi-bar conversion ratio is not going to change so you are doing a bunch of processing everytime you view the data that is not required...

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top