JG Vimalan's Blog

It is all about coding!

Binding DirectoryInfo in TreeView

The following code block will give an idea on binding directoryinfo in TreeView control.

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadLeftNav();
}
}

private void LoadLeftNav()
{
string root = @”D:\Projects”;
DirectoryInfo rootDirectory = new DirectoryInfo(root);
TreeNode rootNode = new TreeNode(rootDirectory.Name);
rootNode.Value = rootDirectory.FullName;
rootNode.ToolTip = rootDirectory.Name;
TreeView1.Nodes.Add(rootNode);
LoadProjects(rootNode, rootDirectory);
}

private void LoadProjects(TreeNode rootNode, DirectoryInfo rootDirectory)
{
DirectoryInfo[] compaines = rootDirectory.GetDirectories();

foreach (DirectoryInfo company in compaines)
{
TreeNode node = new TreeNode(company.Name);
node.Value = company.FullName;
rootNode.ChildNodes.Add(node);
node.ToolTip = company.Name;
this.LoadProjects(node, company);
}
}

Advertisements

September 26, 2011 - Posted by | ASP.NET, C#.NET

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: