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?
 
Ephraim WangoyaConnect With a Mentor Commented:
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
 
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.