If you order the items by ParentCategoryID
, you can perform a second pass to add all the elements with a non-NULL parent category.
Simply add the new items to the Controls
property of the parent.
Usually I use a Dictionary<Key, Value>
to keep track of the existing elements and be able to quickly access them by ID.
Last, if you order the whole data by ParentCategoryID
with NULL
first, then by CategoryID
, you should be able to do it in just one while
loop.