Function for popular Ttreeview only adds Childs to the first Node

Asked

Viewed 901 times

1

I created this procedure to popular a treeview:

procedure TFrmGerProfDock.Button1Click(Sender: TObject);
Var
Tables: TTreeNode;
I: Integer;
begin
  for I := 0 to ds.DataSet.FieldCount - 1 do
  begin
    TreeView1.Items.BeginUpdate;
    TreeView1.Items.Add(nil, ds.DataSet.Fields[I].FieldName); //adiciona um node novo
    while not ds.DataSet.Eof do
    begin
      TreeView1.Items.AddChild(TreeView1.Items[I],
        ds.DataSet.Fields[I].AsString);//adiciona ao node novos childs
      ds.DataSet.Next;
    end;
    ds.DataSet.First;
    TreeView1.Items.EndUpdate;

  end;
end;

The problem is that the procedure always add the Childs in the first Node, what would be wrong in this my code?

1 answer

1


You need to save the parent node reference, use the variable Tables that you created.

Var
  Tables: TTreeNode;
  Root : TTreeNode;
  I: Integer;
begin
  TreeView1.Items.BeginUpdate;
  //Incluir Root
  Root := TreeView1.Items.Add(nil, 'Root'); 
  for I := 0 to ds.DataSet..FieldCount - 1 do
  begin
    // Guarde o node Pai
    Tables := TreeView1.Items.Add(Root, ds.DataSet..Fields[I].FieldName);                
    //adiciona um node novo
    while not ClientDataset1.Eof do
    begin
      //Aqui você inclui no pai
      TreeView1.Items.AddChild(Tables,
        ds.DataSet..Fields[I].AsString);//adiciona ao node novos childs
      ds.DataSet..Next;
    end;
    ds.DataSet..First;   
  end;
  TreeView1.Items.EndUpdate; //O Begin e End update não podem ficar dentro de um loop, isso faz eles ficaram piscando.
end;
  • Exactly, the problem is that they all get separate nodes without a root, there’s how I create a Node and put them all in?

  • Look at my DIT..

  • Thank you very much, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.