I am creating a TreeView which lists all the directories and ".xls" in a given directory
I
have created my own version of TreeView and TreeNode, just to allow my
TreeNodes to have some extra information (path of the file or the
directory)
The problem i am facing is once done the processing the
Tree View does not show mw the nodes in the Tree. if i check the count
it is OK (same no of files and directories i have) but they don't show
up on thew form. So i am providing my code in a hope that some1 will
have an idea what i am doing wrong.
The first part is the method that create the treeview(my version), and tries to copy the nodes from it to the TreeView (.net) .
private void createTree()
{
g_ReportsFolder = Directory.GetCurrentDirectory() + "\\Reports2";
ReportTree rtree = new ReportTree(g_ReportsFolder);
foreach (ReportNode rNode in rtree.Nodes)
{
this.tvReports2.Nodes.Add(rNode);
}
}
Note: If I use
this.tvReports2.Nodes.Add(rNode.Text);
instead of
this.tvReports2.Nodes.Add(rNode);
I can view the nodes in the treeview
This is the TreeView class that creates the list of nodes by using a recursive function createTree(DirectoryInfo p_Directory)
public class ReportTree : TreeView
{
private string ReportsFolder = "";
private DirectoryInfo Directory = null;
public ReportTree(string p_ReportsFolder)
{
ReportsFolder = p_ReportsFolder;
Directory = new DirectoryInfo(ReportsFolder);
createTree(Directory);
string s = this.Name;
}
private void createTree(DirectoryInfo p_Directory)
{
DirectoryInfo l_DirctoryInfo = null;
//Lookout for the children of the Directory
foreach (FileSystemInfo Report in p_Directory.GetFileSystemInfos())
{
if (isDirectory(Report))
{
createDirectoryNode(Report);
l_DirctoryInfo = new DirectoryInfo(Report.FullName);
createTree(l_DirctoryInfo);
}
else if (isXLSFile(Report))
{
createFileNode(Report);
}
else
{
continue;
}
}
}
private bool isDirectory(FileSystemInfo p_FSI)
{
if (p_FSI.Attributes == FileAttributes.Directory)
return true;
else
return false;
}
private bool isXLSFile(FileSystemInfo p_FSI)
{
if (p_FSI.Attributes == FileAttributes.Archive && p_FSI.Extension == ".xls")
return true;
else
return false;
}
private void createDirectoryNode(FileSystemInfo p_DirInfo)
{
string l_Text = p_DirInfo.Name;
string l_Name = p_DirInfo.Name.Replace(" ", "").Replace("'", "").Trim();
string l_Path = p_DirInfo.FullName;
//Create a new Report Node (Customization of the tree node class)
this.Nodes.Add(new ReportNode(true, l_Name, l_Text, l_Path));
}
private void createFileNode(FileSystemInfo p_FileInfo)
{
string l_Text = p_FileInfo.Name.Remove(p_FileInfo.Name.IndexOf(".xls"));
string l_Name = l_Text.Replace(" ", "").Replace("'", "").Trim();
string l_Path = p_FileInfo.FullName;
//Create a new Report Node (Customization of the tree node class)
this.Nodes.Add(new ReportNode(false, l_Name, l_Text, l_Path));
}
}
This is the class that is customization of the TreeNode. I used this to have Some Extra info like path of the file with the node
public class ReportNode : TreeNode
{
public bool isDirectory;
public string NodeName;
public string NodeText;
public string NodePath;
public ReportNode(bool p_isDirectory, string p_Name, string p_Text, string p_Path)
{
this.isDirectory = p_isDirectory;
this.NodeName = p_Name;
this.NodeText = p_Text;
this.NodePath = p_Path;
//adding data to the base (tree Node) because .NET doesn't not to pick up the attributes and override them
this.Text = this.NodeText;
this.Name = this.NodeName;
}
}