i have the below code i want to put in a global method set as static so that i can build a couple tree views on my asp.net web app. now i have each building its own tree view using the code below. i was thinking to make a global static method to generate the node structure such as this and then just assign them to the tree view in my pages… or something like this. i dotn care about the details of the actual solution as long as i can have a single method to call like “buildTree()” that will be able to be used to bind the tree view controls to.
toughs?
DataTable dtProjects = new DataTable();
DataTable dtRelease = new DataTable();
using (SqlConnection con = Global.GetConnection())
{
StringBuilder str = new StringBuilder();
str.Append("SELECT r.ReleaseId, ");
str.Append(" r.Name, ");
str.Append(" rs.EndDate ");
str.Append(" FROM Release r ");
str.Append(" LEFT OUTER JOIN ReleaseSchedule rs ");
str.Append(" ON r.ReleaseId = rs.ReleaseId ");
str.Append(" AND rs.MilestoneCID = 77");
str.Append(" WHERE r.CompletionStatusCID NOT IN (34, 35) ");
str.Append(" ORDER BY r.ReportingPriority, r.Name ");
SqlDataAdapter da = new SqlDataAdapter(str.ToString(), con);
da.Fill(dtRelease);
str = new StringBuilder();
str.Append("SELECT p.ProjectId, ");
str.Append(" p.ProjectName, ");
str.Append(" p.ParentProjectId, ");
str.Append(" p.ReleaseId ");
str.Append(" FROM Project p ");
str.Append(" WHERE p.CompletionStatusCID NOT IN (34, 35) ");
str.Append(" AND p.ProjectTypeCID <> 92 ");
str.Append(" ORDER BY p.ProjectName ");
da = new SqlDataAdapter(str.ToString(), con);
da.Fill(dtProjects);
}
tvProject.Nodes.Clear();
TreeNode rootNode = new TreeNode("All Projects");
rootNode.Expanded = true;
foreach (DataRow drRelease in dtRelease.Rows)
{
TreeNode releaseNode = new TreeNode((string)drRelease["Name"]);
DataRow[] releaseProjects = dtProjects.Select("(ReleaseId = " + drRelease["ReleaseId"] + ") AND (ParentProjectId IS NULL)");
foreach (DataRow drProject in releaseProjects)
{
TreeNode projectNode = new TreeNode((string)drProject["ProjectName"], drProject["ProjectId"].ToString());
projectNode.ToolTip = "This is a project node and is selectable";
loadTVNode((int)drProject["ProjectId"], projectNode, dtProjects);
if (drRelease["EndDate"] != System.DBNull.Value)
{
string s = ((DateTime)drRelease["EndDate"]).ToString(Global.CONST_DateFormat);
releaseNode.ToolTip = "Release scheduled for deployment on " + s;
}
else
releaseNode.ToolTip = "Release deployment has not been scheduled";
releaseNode.ToolTip += ". This node is not selectable";
releaseNode.ChildNodes.Add(projectNode);
}
rootNode.ChildNodes.Add(releaseNode);
}
tvProject.Nodes.Add(rootNode);
I didn’t get the question right away, but I guess you’re looking for something like this:
Create a class called TreeViewBuilder with just a public method to return the filled TreeView from an existing one:
My guessing is that you can fill the blanks in this solutions, like passing those parameters you need and so on. Also you can build the code for the tree methods being called from the code you placed in the question. You creation routine would be now:
Even more cool, if you are using .NET framework over 3.5 you can make the method as an extension:
Then, calling your method would be so much simpler:
And it’s done! MSDN has some great codes for extension methods.
Hope I can help!