1

I have some code on a form which creates a bunch of labels from the column names in the database

I am trying to write another piece of code that uses a FDQuery to select all the records and then create labels based on the value in each row

Currently i have the following.

  while not FDQuery1.Eof do
  begin
    while recordCount < colTotal do
    begin
      newLabel := TLabel.Create(Self);
      newLabel.Parent := panel3;
      newLabel.Align := TAlignLayout.Top;
      newLabel.Text := FDQuery1.FieldByName('Torque2').AsString;
      newLabel.Margins.Top := 10;
      newLabel.Margins.Left := 10;
      newLabel.Margins.Right := 10;
      inc(recordCount);
      FDQuery1.Next;
    end;
  end;

How can i make this create a label with the result of each row dynamically without me needing to actually put the column name like "torque2" as i have here.

So for example on my form the labels will create as follows

row1 
row2 
row3
row4
row5 

because right now this code just simply loops one row value

Thanks

Victoria
  • 7,822
  • 2
  • 21
  • 44
Revski
  • 13
  • 4

1 Answers1

7

Instead of name based FieldByName method you can access tuple values by the Fields property which is a 0 based indexed collection property.

FireDAC offers more efficient way for accessing data though. Once you have all tuples fetched on the client, you can iterate through the internal data storage this way:

var
  S: string;
  Row, Col: Integer;
begin
  for Row := 0 to FDQuery1.Table.Rows.Count - 1 do
    for Col := 0 to FDQuery1.Table.Columns.Count - 1 do
      S := FDQuery1.Table.Rows[Row].GetData(Col);
end;

That's IMHO easier to read and also saves quite a lot of time because it doesn't move the dataset cursor. Disadvantage might be that you need to have tuples fetched on the client side.

Victoria
  • 7,822
  • 2
  • 21
  • 44
  • Thanks!! Cant believe i didn't see this before – Revski Jun 10 '18 at 14:44
  • You're welcome! Anyway, try to consider using some layer, like e.g. [TGridPanelLayout](http://docwiki.embarcadero.com/Libraries/en/FMX.Layouts.TGridPanelLayout). It makes your task (building a label based grid) far much easier. And except this iteration FireDAC offers internal storage iteration which is more efficient and easier to read in code. If you were interested, I can add an example to this post. – Victoria Jun 10 '18 at 14:50
  • Please do i am interested to see – Revski Jun 10 '18 at 16:18
  • I've added example of internal storage iteration. [Here is a method](https://pastebin.com/DwHqtjKQ) that can populate FireDAC dataset object into a `TGridPanelLayout` control. – Victoria Jun 10 '18 at 16:56
  • I hadn't come across the Rows & Columns properties before, so +1 for mentioning them. – MartynA Jun 10 '18 at 18:35
  • 1
    @MartynA, FireDAC's internal storage is pretty powerful, and efficient. Actually, it seems to be inspired by the C# implementation of the [DataTable](https://msdn.microsoft.com/en-us/library/system.data.datatable(v=vs.110).aspx) class (many things does in the same way). Hence questions [like this one](https://stackoverflow.com/q/50569010/8041231) immediately brings to my mind answers like _"Blame something else, not FireDAC"_ :) – Victoria Jun 10 '18 at 18:51