Delphi 7 - Populating TreeView with SQL Query?

The query would be:
qry.SQL.Add('SELECT id, title, priority FROM inbox  ');
qry.ExecSQL;

how can I populate the contents of the query in a treeview? Dynamically creating the necessary columns (id, title, priority).

Thanks
John86aAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimyXCommented:
First, you use open not execsql:
qry.SQL.Add('SELECT id, title, priority FROM inbox  ');
qry.open;

You use ExecSQL when you have an SQL statments that will not return any rows, such as update, delete, alter.

Can you elaborate more please, what do you mean by "Dynamically creating the necessary columns" do you mean you want to add them in the TreeView?
0
Ephraim WangoyaCommented:
Remember a TreeView has only the Text Property for the node, What kind of format do you want to display

Unless you mean it in TListView

For TreeView try
var
  Node: TTreeNode;
begin
  qry.SQL.Add('SELECT id, title, priority FROM inbox  ');
  qry.Active := true;

  TreeView1.Items.Clear;

  while not qry.Eof do
  begin
    Node := TreeView1.Items.Add(nil, '');
    Node.Text := qry.FieldByName('id').AsString + ',  ' +
                 qry.FieldByName('title').AsString + ',  ' +
                 qry.FieldByName('priority').AsString ;
    qry.Next;
  end;
end;

Open in new window

0
John86aAuthor Commented:
Sorry, I meant via programming.
I.e. (fictional example)
treeview.AddColumn('id');
treeview.AddColumn('title');
treeview.AddColumn('priority');

then populate the rows with the content fetched from the SQL query.
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Ephraim WangoyaCommented:
if you mean a TListView, do the following
var
  Item: TListItem;
  Col: TListColumn;
begin
  qry.SQL.Add('SELECT id, title, priority FROM inbox  ');
  qry.Active := true;

  ListView1.Items.Clear;
  ListView1.ViewStyle := vsReport;
  Col := ListView1.Columns.Add;
  Col.Caption := 'ID';

  Col := ListView1.Columns.Add;
  Col.Caption := 'TITLE';

  Col := ListView1.Columns.Add;
  Col.Caption := 'Priority';

  while not qry.Eof do
  begin
    Item := ListView1.Items.Add;
    Item.Caption := qry.FieldByName('id').AsString;
    Item.SubItems.Add(qry.FieldByName('title').AsString);
    Item.SubItems.Add(qry.FieldByName('priority').AsString);
    qry.Next;
  end;
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ephraim WangoyaCommented:

As I said before, TreeView has no columns, you probably want to use TListView as in my last post #35314732
0
John86aAuthor Commented:
Exactly what I meant, sorry.
Thanks!
0
jimyXCommented:
If you need to add the columns to the TreeView then:
  qry.SQL.Add('SELECT id, title, priority FROM inbox ');
  qry.open;
  for i := 0 to qry.FieldCount-1 do
    begin
      TreeView1.Items.Insert(TreeView1.Items.GetFirstNode, qry.Fields[i].FieldName);
    end;

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.