Page 1 of 1

TMyQuery and populating a TTreeView

Posted: Sun 18 Feb 2007 18:45
by Bryan
Hello,

I'm trying to populate a TTreeView with the results of a TMyQuery.
The query returns a single list of items (1 field)

Code: Select all

  for x := 0 to MyQuery1.RecordCount - 1 do
    TreeView1.Items.AddChild(MainNode,  MyQuery1.FieldByName('storm_id')[x].AsString);
Obviously this will not compile.... so is there a way.
This seems so simple, but I've been pounding my head for over 2 hours.

Thanks!
Bryan

Posted: Mon 19 Feb 2007 07:44
by Antaeus
You are trying to access a recordset as an array of records. This is not correct. Try to use the following code:

Code: Select all

  MyQuery1.First;
  for x := 0 to MyQuery1.RecordCount - 1 do begin
    TreeView1.Items.AddChild(MainNode,  MyQuery1.FieldByName('storm_id').AsString);
    MyQuery1.Next;
  end;
Please read more about working with datasets in the Delphi Help documentation.

Posted: Mon 19 Feb 2007 13:33
by Bryan
Thanks for your help. I finally figured it out after another hour... :) Your code example is a little more streamlined so I'll use it instead of my attempt.

Thanks!
Bryan
Antaeus wrote:You are trying to access a recordset as an array of records. This is not correct. Try to use the following code:

Code: Select all

  MyQuery1.First;
  for x := 0 to MyQuery1.RecordCount - 1 do begin
    TreeView1.Items.AddChild(MainNode,  MyQuery1.FieldByName('storm_id').AsString);
    MyQuery1.Next;
  end;
Please read more about working with datasets in the Delphi Help documentation.