TreeView question: How can i implement this? (need help!!!)


Hi folks,

I have a MS Access that contains a table called CUSTOMERS.
The table CUSTOMERS contains 3 fiels: NAME, DATE and PRODUCTS. The field products store the products that the CUSTOMER bought. Ok. This is my database and my table.

My question is: how i put that information in a TreeView object? More clearly, the TREEVIEW must be similar to:

+ CUSTOMER 1
+ CUSTOMER 2
...

If the guy click on the plus signal of the CUSTOMER 1, the TREEVIEW change to:

+ CUSTOMER 1
- Name
- Date
+ PRODUCTS
+ CUSTOMER 2

If the guy click on the plus signal of the PRODUCTS, the TREEVIEW change to:

+ CUSTOMER 1
- Name
- Date
- PRODUCTS
- PRODUCT 1
- PRODUCT 2
+ CUSTOMER 2


Do you imagine how can i do that? Please, if you know some component, i deeply ask you for dont only put the name, but the example too, because i´ve tried many components and i didnt get work with them.

Thanks very much and best regards.

joelsilvaAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
DavidBirch2dotComConnect With a Mentor Commented:
To use a virtual tree view you need to do a few things,

first declare a new type for holding data for the nodes asimple example would be

type

  PMyRec = ^TMyRec; // create a pointer for this type - very important
  TMyRec = record
    Caption: WideString; // add any othervaribles here, you probably only need a caption feild
  end;

then you need to set the nodedatasize for the treeview on create add something like
  treeview.NodeDataSize := SizeOf(TMyRec);


To set the caption of the node then use the treeview event OnGetText to set the caption something like

var
  Data: PMyRec;

begin
  Data := Sender.GetNodeData(Node);
  if Assigned(Data) then
    Text := Data.Caption;
end;

you could then set the caption or text depending on which customer it is

To add nodes make a function similar to this

function Addnode(Addtype: integer; Addernode: Pvirtualnode): Pvirtualnode;
Begin
   Addnode:= Addernode;
   with mytree do
   Begin

   Case addtype of
   0: // root
   Begin
      RootnodeCount:= RootnodeCount+1;
      Addnode:= Rootnode.LastChild;
   end;
   1: // child
   begin
      ChildCount[Addernode] := ChildCount[Addernode] + 1;
      Expanded[Addernode] := True;
      InvalidateToBottom(Addernode);
      Addnode:= Addernode.LastChild;
   end;
   2: // sibblin
   Begin
      Addernode:= Addernode.Parent;
      ChildCount[Addernode] := ChildCount[Addernode] + 1;
      Expanded[Addernode] := True;
      InvalidateToBottom(Addernode);
      Addnode:= Addernode.LastChild;
   end;
   end; //case

   end;  //with

end;

To access the data on a node use

var
  Data: PMyRec;

begin
  Data := mytree.GetNodeData(Node);

and then you can access it, I

 hope that that helps

DAvid
0
 
calinutzCommented:
I would not recomend you to do that because it woul slow down your entire application. Especially when you want to populate the treeview. It will be a pain.
  You could do it simple with the TreeView component from delphi, or you could try other (more complicated to use) components... such as VirtualTreeView from
http://www.soft-gems.net/VirtualTreeview/VT.php

I would suggest that you search for another way to present your data.
Cheers.
0
 
joelsilvaAuthor Commented:
Hi Calinutz, thx for your comments.

As i told, i know many components related to TreeView and I know VIRTUAL TREEVIEW too (it is installed here), but i dont know how implement what i need with that. I´ve already looked forward resources and didnt find. So, do you know how can i do what i need with VIRTUAL TREEVIEW?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
joelsilvaAuthor Commented:
I dont have Delphi here, but i will test your code tonight... Thx. very much DAVID.
0
 
joelsilvaAuthor Commented:
Hi David,

I did exactly what you recommended and I got the error here:

==================================================
var
  Data: PMyRec;
begin
  Data := mytree.GetNodeData(Node); //  HERE is the ERROR: NODE was UNDECLARED           ==================================================


I disponibilize the PROJECT that i did (http://www.cordeirorodrigues.com.br/download/01.zip).

As I said, i need take data from my MS Access database and show them in a TreeView. Could you give me a exemple (with any MS Access or Interbase database), using my PROJECT? Could be something very simple, but showing the formation extracted from a database file. I don´t know how implement this using VIRTUAL TREEVIEW.

If you cannot do that, no problem.

Best regards.
0
 
DavidBirch2dotComCommented:
ok have a look @ this see if this helps you

www.davidbirch2.com/hidden/test.zip

David
0
 
joelsilvaAuthor Commented:
Hey DAVID, you got it!!! Thx. and thx. very much for that.

Just one point: Could you add another NODE for me, pls? So, i think that i will get implement everything that i need.

Something like:

- CUSTOMER := ????
  - Name := ????
  - Date := ????
  - Node 1  // Please, could you put this?
    - Node 1.1  // Please, could you put this?
    - Node 1.2  // Please, could you put this?

- CUSTOMER := ????  // Please, could you put this?
  - Name := ????  // Please, could you put this?
  - Date := ????  // Please, could you put this?
  - Node 1  // Please, could you put this?
    - Node 1.1  // Please, could you put this?
    - Node 1.2  // Please, could you put this?
  - Date := ????
  - Node 1  // Please, could you put this?
    - Node 1.1  // Please, could you put this?
    - Node 1.2  // Please, could you put this?
0
 
DavidBirch2dotComCommented:
ROLF :) (Rolling On The Floor Laughing) - // Please, could you put this?

right, well thats why I left he  for loop in comments for you, if you can write the loop to get all the records that will deal with making a new node + children for each customer.  I didnt add it as it would depend greatly on how you are using your database...

As for adding

- CUSTOMER := ????
  - Name := ????
  - Date := ????
  - Node 1  // Please, could you put this?
    - Node 1.1  // Please, could you put this?
    - Node 1.2  // Please, could you put this?

you can add additional nodes on the same level as name and date by adding the code

   childnode:= Addnode(1,Parentnode); // add another child
   Data := VirtualStringTree1.GetNodeData(childnode);
   Data.Caption:= ' feild name:= ???'; // data in feild one

to get to the level below that -  try the code

   childnode:= Addnode(1,Parentnode.lastchild); // add another child to the last child of the customer node
   Data := VirtualStringTree1.GetNodeData(childnode);
   Data.Caption:= 'my child:= ???'; // data in feild one

I think that solves the problem?

DAvid
0
 
DavidBirch2dotComCommented:
opps we are forgetting to clear the memory, add the following code on the OnFreeNode event

var
  Data: PMyRec;

begin
  Data := Sender.GetNodeData(Node);
  if Assigned(Data) then
    Data.Caption := '';
end;
0
 
joelsilvaAuthor Commented:
Cool! Now i can improve what i need... Thank you very much man.

Let me see if i perfectly understood:

Where you put "CUSTOMER:= ????" i must add some code there to take information from my database, isn´t? So, maybe will be:

=======================================
CUSTOMER:= ADOQuery1.FieldByName('customer').AsString
=======================================

Am i correct? I mean, i understood perfectly your code. I just want confirm that i could integrate your code with my database.

0
 
DavidBirch2dotComCommented:
yeah somthing like that

Data.Caption:= ADOQuery1.FieldByName('customer').AsString;

would do i think

David

0
 
joelsilvaAuthor Commented:
I deeply thank you very much for your attention and precious comments.
0
All Courses

From novice to tech pro — start learning today.