Populating TreeView list from table

Hi All,

I need to populate tree view from the database table.
Example Hierarchy is like this..
We have all the brokers at the top level. Under the brokers we have clients. Under the cients we have trasaction types which he deals on(cash,margin,spot etc..).

Sample Table:

BrokerCode                  Client code          TransType
=======                   =======          =======
001                              Client1                Cash
001                              Client1                Margin
001                              Client2                Margin
002                              Test                   Spot
003                              Test1                 Cash
003                              Test2                 Cash

Output should be something like this....

-- 001
       --Client1
              --Cash
              --Margin
       --Client2
              --Margin
-- 002
       --Test
              --Spot


Assume that i have all the rows in a datawindow or datastore. Please give me the code from there on.

Thanks in advance
Srinib












LVL 1
srinibAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
diasroshanConnect With a Mentor Commented:
hi,

paste the following code in the constructor event of the treeview...

TreeViewItem      ltvi_Menu_Item
Long            ll_Parent_Handle,ll_Handle, ll_tvi,i
String             ls_name


tv_1.DisableDragDrop      = True

            Dw_3.SetTransObject(SQLCA)
            dw_3.Retrieve(0) // This dw is as per the table design given earlier and retrieval argument is the parentid

            
            For i = 1 to dw_3.Rowcount()
                  ls_name = dw_3.Object.datanm[i]
                  ltvi_Menu_Item.Label = ls_name
                  ltvi_Menu_Item.Data  = 0
                  ltvi_Menu_Item.pictureindex = 1
                  ltvi_Menu_Item.Selectedpictureindex = 1
                  ltvi_Menu_Item.Children      = True
                  ll_Parent_Handle = tv_1.InsertItemFirst (0,ltvi_Menu_Item)
            Next      
      //constructor code ends here...



//clicked event code goes here...      

TreeViewItem      ltvi_Menu_Item
long ll_tvi, ll_tvparent, i, ll_data
String ls_name, ls_data


ll_tvparent = tv_1.FindItem(roottreeitem!,0)
ll_tvi = tv_1.FindItem(currenttreeitem! , 0)


ll_data = tv_1.Getitem(handle,ltvi_Menu_Item)

ls_data = ltvi_Menu_Item.Label

select id into :ll_data
from test
where datanm = :ls_data;

dw_3.retrieve(ll_data)

For i = 1 To dw_3.Rowcount()      
            ls_name = dw_3.Object.datanm[i]
            ltvi_Menu_Item.Label = ls_name
            ltvi_Menu_Item.Data  = 2
            ltvi_Menu_Item.pictureindex = 2
            ltvi_Menu_Item.Selectedpictureindex = 1
//            ltvi_Menu_Item.Children      = False
//            ltvi_Menu_Item.level = 2
            tv_1.InsertItemSort (Handle,ltvi_Menu_Item)

//      tv_1.InsertItem(handle,1,ls_name,2)
Next      


// clicked code ends here...


Cheers,
Rosh
0
 
diasroshanCommented:
hi,
i guess u need a new table and the format should be as follows to get a treeview....

ID         DATA    PARENTID
1      001      0
2      002      0
3      003      0
4      Client1      1
5      Client2      1
6      Test      2
7      Test1      3
8      Test2      3
9      Cash      4
10      Margin      4
12      Spot      6
13      Cash      7
14      Cash      8

i will paste the code to make the treeview... i need to write it... it might take some time coz i'm a bit tied up...

Also refer PB help..

Cheers,
Rosh
0
 
srinibAuthor Commented:
Please paste the code fi you have time.

Thanks for taking your valuable time.

Thanks again
Srinib
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
srinibAuthor Commented:
Please paste the code if you have time.

Thanks for taking your valuable time.

Thanks again
Srinib
0
 
bluecometCommented:
What i suggest you for performance to populate a treeview to do it level 1 by 1 if you don't need to expand all on first open event.

so in your example:

do first sql:

select id_brokercode into :li_id from "yourtable";

put in a datastore. With rowcount() you know how many you have.

go throw it with a for next

for i = 1 to ds_1.rowcount()

   tvi_item.label = "ID Broker 1"
   tvi_item.data = li_id
  // you can also put pictureindex to a picture
   tv.insertitemlast(your_handle,tvi_item)  // here your handle is 0 that is the top level
next

on every expand item you do same thing but you change your sql and the handle.


If you must have an expand all on first open event then you must do it with for in a for etc...

that's it.
0
 
srinibAuthor Commented:
Please paste the code as per Rosh table design.

Thanks
Srinib
0
 
srinibAuthor Commented:
Yes, It is populating the tree view now.
I need small extension to this code since i am facing one minor problem here.

1) It is populating items on the first click, and also it is populating again all the items on the second click also. My aim is, it should populate only once in first click.

2) Should be able to collapse and expand the items depending on the present state.

Thanks again
Srinib
0
 
sandhiyaaCommented:
reset the datawindow and retrieve the records again

regards,
sandhiya
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.