In the future article we will demonstrate how to use Ajax with the TreeView Treeview control is the most used server control for representing tree view hierarchy data structure. We also learned how to iterate through the folder structure and list all the files and the folders recursively. In this article we learned how to populate the TreeView control of depth N. The following code uses the BuildDirectoryTree method. Each directory is sent back to the BuildDirectoryTree method to be populated again. Inside the method the TreeNode is populated with the current files in the directory and also the current directories inside the root directory. The BuildDirectoryTree takes in the path of the root folder and an empty TreeNode instance. The recursive method for this scenario is little different and shown below: Instead of starting at the C: we will start at C:\Projects which contains some Visual Studio projects. In this section we are going to implement it. In the beginning of the article we referenced towards the directory structure and how it can be displayed in the TreeView control. Populating Directory Structure for TreeView Control: In the next section we will use the directory structure to populate our TreeView control. The screenshot of the TreeView control is shown below: The following method utilizes the BuildTree method and populates the TreeView control. You can use an array of fields to look for in the object instance. For the sake of simplicity we are using "Name" as a common field to fetch from each object. The BuildTree method is called in a recursive fashion to populate the TreeNode object. Now, let's take a look at the recursive method which is responsible for populating the TreeNode. The following implementation shows a hierarchy.Īs, you can see in the code above each category consists of a product, each product consists of a sub product, each sub product consists of a sub sub product and this can go on forever. The C drive contains folders and then each folder has files and sub folders and then each sub folder has other folders and files and this continues to N levels of folders.įirst, we will create such a N level hierarchy and then assign it to the TreeView control. Consider a folder structure on your local hard drive. The concept of N level nesting revolves around the fact that a TreeView control should be able to display objects which have a detailed nested hierarchy. Populating TreeView with N Level Nesting: In the next section we are going to show how to achieve N level nesting with the use of recursion. We need to make sure that the TreeView is able to populate with N level of nested objects. Take a look at the following screen shot which shows the nested TreeView in action.Īlthough the TreeView is displayed and function correctly but it is limited to one level of nesting. The CollapseAll method is invoked to make sure that all nodes are collapsed when displayed on the screen. Finally, the nodes are added to the TreeView control. Then inside the category instance we iterate through the products and populate the child nodes to the existing TreeNode. Inside BindDataSimple method we iterate through the collection and create TreeNode. The BindDataSimple method simply calls the DataAccess.GetCategories which returns a List filled with dummy data. The code used to populate the TreeView control is shown below: The two classes used are "Category" and "Product" and they are defined below:Īlthough the Product class exposes SubProducts but we are not going to use it for this example. In this example we will use a one level nested structure. Let's first take a look at simple example in which we will demonstrate how to populate a TreeView control without using recursion.
0 Comments
Leave a Reply. |