I have a linq object that i’m trying to fill a treeview from using c# winforms.
Sample
Column 1 Column 2 Column 3
Test 1 Item A Sub Item 1
Test 1 Item A Sub Item 2
Test 1 Item A Sub Item 3
Test 1 Item B Sub Item 1
Test 1 Item B Sub Item 2
Test 1 Item B Sub Item 3
And fill the treeview like
Test 1 (CheckBox)
------Item A (CheckBox)
-------------Sub Item 1 (CheckBox)
-------------Sub Item 2 (CheckBox)
-------------Sub Item 3 (CheckBox)
------Item B (CheckBox)
-------------Sub Item 1 (CheckBox)
-------------Sub Item 2 (CheckBox)
-------------Sub Item 3 (CheckBox)
What is the easiest way to achieve this?
Thanks
Sp
Well, if you perform the following query:
You will have all of the items grouped first by column1, then 2 and 3, so It’s already in a tree structure. Now you just need to have 3 nested
forloops to add the items to the treeview.Clearly this code only works if there are a fixed number of columns with a fixed (max) depth. If you don’t know the number of columns and can have an arbitrary depth then you’d need a fairly radically different approach.