Topic: Just sharing: extracting data from a tablegrid

I didn't know about this feature in MVD. I discovered that it works and met my needs. Sharing.


procedure frm_grid_zoom_TableGrid_subnotas_OnChange (Sender: TObject);
begin
   frm_grid_zoom.Edit_med.Value :=
       sqlexecute('select avg("tbl_grid.valor") from ('+frm_grid_zoom.TableGrid_subnotas.dbGetSqlStatement+')');
   frm_grid_zoom.Edit_min.Value :=
       sqlexecute('select min("tbl_grid.valor") from ('+frm_grid_zoom.TableGrid_subnotas.dbGetSqlStatement+')');
   frm_grid_zoom.Edit_max.Value :=
       sqlexecute('select max("tbl_grid.valor") from ('+frm_grid_zoom.TableGrid_subnotas.dbGetSqlStatement+')');
end;
Roberto Alencar

Re: Just sharing: extracting data from a tablegrid

+1

Domebil

Re: Just sharing: extracting data from a tablegrid

Hi jrga, domebil


Sometimes you can try it this way. You don't even have to display the footer.
Just indicate the correct column number.

procedure frm_grid_zoom_TableGrid_subnotas_OnChange (Sender: TObject);
begin
  frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.FormulaKind := fkMinimum;
  frm_grid_zoom.TableGrid_subnotas.CalculateFooter;
  frm_grid_zoom.Edit_min.text := frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.Caption;
  frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.FormulaKind := fkMaximum;
  frm_grid_zoom.TableGrid_subnotas.CalculateFooter;
  frm_grid_zoom.Edit_max.text := frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.Caption;
  frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.FormulaKind := fkAverage;
  frm_grid_zoom.TableGrid_subnotas.CalculateFooter;
  frm_grid_zoom.Edit_med.text := frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.Caption;
  frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.FormulaKind := fkNone;
end;  

4 (edited by jrga 2026-04-23 22:12:54)

Re: Just sharing: extracting data from a tablegrid

sparrow wrote:

Hi jrga, domebil


Sometimes you can try it this way. You don't even have to display the footer.
Just indicate the correct column number.

procedure frm_grid_zoom_TableGrid_subnotas_OnChange (Sender: TObject);
begin
  frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.FormulaKind := fkMinimum;
  frm_grid_zoom.TableGrid_subnotas.CalculateFooter;
  frm_grid_zoom.Edit_min.text := frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.Caption;
  frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.FormulaKind := fkMaximum;
  frm_grid_zoom.TableGrid_subnotas.CalculateFooter;
  frm_grid_zoom.Edit_max.text := frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.Caption;
  frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.FormulaKind := fkAverage;
  frm_grid_zoom.TableGrid_subnotas.CalculateFooter;
  frm_grid_zoom.Edit_med.text := frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.Caption;
  frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.FormulaKind := fkNone;
end;  

Hi Sparrow, thanks for providing this alternative method! I followed this approach because the table grid already had the total for the "value" column in the footer, and I wanted to display the average, minimum, and maximum values.

Roberto Alencar

Re: Just sharing: extracting data from a tablegrid

Hello All,
Yet another variation on the same theme is to use the tablegrid's in-built footer function and then hide the footer.
The footer values (count, sum, minimum, maximum, average, distinct) can then simply be copied into edit fields.
Derek.

Post's attachments

Attachment icon Mileage.zip 439.63 kb, 144 downloads since 2026-04-24 

Re: Just sharing: extracting data from a tablegrid

derek wrote:

Hello All,
Yet another variation on the same theme is to use the tablegrid's in-built footer function and then hide the footer.
The footer values (count, sum, minimum, maximum, average, distinct) can then simply be copied into edit fields.
Derek.


Hi Derek, that's a great alternative too! Thanks!

Roberto Alencar

Re: Just sharing: extracting data from a tablegrid

jrga wrote:
sparrow wrote:

Hi jrga, domebil


Sometimes you can try it this way. You don't even have to display the footer.
Just indicate the correct column number.

procedure frm_grid_zoom_TableGrid_subnotas_OnChange (Sender: TObject);
begin
  frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.FormulaKind := fkMinimum;
  frm_grid_zoom.TableGrid_subnotas.CalculateFooter;
  frm_grid_zoom.Edit_min.text := frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.Caption;
  frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.FormulaKind := fkMaximum;
  frm_grid_zoom.TableGrid_subnotas.CalculateFooter;
  frm_grid_zoom.Edit_max.text := frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.Caption;
  frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.FormulaKind := fkAverage;
  frm_grid_zoom.TableGrid_subnotas.CalculateFooter;
  frm_grid_zoom.Edit_med.text := frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.Caption;
  frm_grid_zoom.TableGrid_subnotas.Columns[0].Footer.FormulaKind := fkNone;
end;  

Hi Sparrow, thanks for providing this alternative method! I followed this approach because the table grid already had the total for the "value" column in the footer, and I wanted to display the average, minimum, and maximum values.

https://i.postimg.cc/qqq1B4zY/tablegrid-mvd.png

Roberto Alencar

Re: Just sharing: extracting data from a tablegrid

Hi Roberto


Did everything work out?
If you want to display TOTAL in your footer, the full code will look like this, assuming I calculated the table columns correctly. The result of the last formula will remain in the footer.

procedure frm_grid_zoom_TableGrid_subnotas_OnChange (Sender: TObject);
begin
  frm_grid_zoom.TableGrid_subnotas.Columns[5].Footer.FormulaKind := fkMinimum;
  frm_grid_zoom.TableGrid_subnotas.CalculateFooter;
  frm_grid_zoom.Edit_min.text := frm_grid_zoom.TableGrid_subnotas.Columns[5].Footer.Caption;
  frm_grid_zoom.TableGrid_subnotas.Columns[5].Footer.FormulaKind := fkMaximum;
  frm_grid_zoom.TableGrid_subnotas.CalculateFooter;
  frm_grid_zoom.Edit_max.text := frm_grid_zoom.TableGrid_subnotas.Columns[5].Footer.Caption;
  frm_grid_zoom.TableGrid_subnotas.Columns[5].Footer.FormulaKind := fkAverage;
  frm_grid_zoom.TableGrid_subnotas.CalculateFooter;
  frm_grid_zoom.Edit_med.text := frm_grid_zoom.TableGrid_subnotas.Columns[5].Footer.Caption;
  frm_grid_zoom.TableGrid_subnotas.Columns[5].Footer.FormulaKind := fkSum;
  frm_grid_zoom.TableGrid_subnotas.CalculateFooter;
end; 

Re: Just sharing: extracting data from a tablegrid

sparrow wrote:

Hi Roberto


Did everything work out?
If you want to display TOTAL in your footer, the full code will look like this, assuming I calculated the table columns correctly. The result of the last formula will remain in the footer.

procedure frm_grid_zoom_TableGrid_subnotas_OnChange (Sender: TObject);
begin
  frm_grid_zoom.TableGrid_subnotas.Columns[5].Footer.FormulaKind := fkMinimum;
  frm_grid_zoom.TableGrid_subnotas.CalculateFooter;
  frm_grid_zoom.Edit_min.text := frm_grid_zoom.TableGrid_subnotas.Columns[5].Footer.Caption;
  frm_grid_zoom.TableGrid_subnotas.Columns[5].Footer.FormulaKind := fkMaximum;
  frm_grid_zoom.TableGrid_subnotas.CalculateFooter;
  frm_grid_zoom.Edit_max.text := frm_grid_zoom.TableGrid_subnotas.Columns[5].Footer.Caption;
  frm_grid_zoom.TableGrid_subnotas.Columns[5].Footer.FormulaKind := fkAverage;
  frm_grid_zoom.TableGrid_subnotas.CalculateFooter;
  frm_grid_zoom.Edit_med.text := frm_grid_zoom.TableGrid_subnotas.Columns[5].Footer.Caption;
  frm_grid_zoom.TableGrid_subnotas.Columns[5].Footer.FormulaKind := fkSum;
  frm_grid_zoom.TableGrid_subnotas.CalculateFooter;
end; 

Hi Sparrow, everything went well and thank you again.

Roberto Alencar