Monday, January 9, 2012

Tree view in asp.net using csharp

The control is used for displaying the information in hierarchical structure.
ex:solution explorer
you can construct a tree in two ways
1.manual construction using tree node editor
2.pragmatically construction

Manual construction using tree editor:

If you want to construct a tree using a tree node editor ,go to the properties of tree view and select nodes properties which displays you button beside it.click on it open tree node editor.Use the add root and add child buttons to adding the root nodes and child nodes according to requirements
Place a Tree view control on the form and construct the tree as fallowing using tree node editor
Dock Property:
Every control provides the property known as DOCK property which is used to specify the way the control has to be placed on the form .it takes any of 6 different values-->None,Left,Right,Top,Bottom,Fill

None:it is used a fixed width and height as well as uses fixed X and Y coordinating to place itself on the form which will not be changing all at the run time
if set as left,right,top or bottom the control sits on the selected option irrespective of the form size as well as adjusts according to the form size

if set as fill the control occupies the complete size of the form with out room for any other controls
Every node under a tree is referred by using it's index position as fallowing
Nodes[0]
Nodes[0].Nodes[0]
Nodes[0].Nodes[0].Nodes[0]
Nodes[0].Nodes[1]
Nodes[0].Nodes[1].Nodes[0]
Construct a tree view Pragmatically:

If you want to construct a tree view pragmatically we require to construct each node explicitly and add it user the tree view control using the node start add method
Nodes.Add(string text)
Place tree view control on the form and set DOCK property as left and write the fallowing code in the form load
TreeView1.Nodes.Add("Home");
TreeView1.Nodes[0].Nodes.Add("About");
TreeView1.Nodes[0].Nodes[0].Nodes.Add("Portfolio");
TreeView1.Nodes[0].Nodes.Add("Contact us");
TreeView1.Nodes[0].Nodes[1].Nodes.Add("Location");
TreeView1.ExapandAll();
Here i will show an example for tree view
Using System.IO;
namespace windowsproject
{
publec partial class Forms:Form
//form load
Treeview1.Nodes.Add("c://");
string[]florders=Directory.GetDirectoroies(c://")
foreach(string fpath in folders)
{
string fname=fpath.substring(3);
Treeview1.Nodes[0].Nodes.Add(fname);
}
Treeview1.ExapndAll();
}
Post a Comment

Bel