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

1
  • FdQuery1.Fields property has a collection for it. Did you try it? Commented Jun 10, 2018 at 15:52

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks!! Cant believe i didn't see this before
You're welcome! Anyway, try to consider using some layer, like e.g. 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.
Please do i am interested to see
I've added example of internal storage iteration. Here is a method that can populate FireDAC dataset object into a TGridPanelLayout control.
@MartynA, FireDAC's internal storage is pretty powerful, and efficient. Actually, it seems to be inspired by the C# implementation of the DataTable class (many things does in the same way). Hence questions like this one immediately brings to my mind answers like "Blame something else, not FireDAC" :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.