我有下面的代码我想把一个全局方法设置为静态,以便我可以在我的asp.net Web应用程序上构建几个树视图。现在我使用下面的代码构建自己的树视图。我正在考虑制作一个全局静态方法来生成这样的节点结构,然后将它们分配给我的页面中的树视图......或类似的东西。我想关注实际解决方案的细节,只要我可以使用单个方法调用“buildTree()”,它将能够用于将树视图控件绑定到。
恶棍?
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);
答案 0 :(得分:0)
我没有马上得到这个问题,但我想你正在寻找这样的事情:
使用一个公共方法创建一个名为TreeViewBuilder的类,以从现有方法返回填充的TreeView:
public class TreeViewBuilder
{
public static TreeView BuildTreeView( TreeView tree )
{
DataTable projects = GetProjects();
DataTable releases = GetRealeases();
return InternalTreeViewBuilder(tree, projects, releases);
}
}
我的猜测是,您可以填写此解决方案中的空白,例如传递您需要的参数等等。您还可以为您在问题中放置的代码调用树方法构建代码。你现在创建例程:
//TreeView something;
TreeViewBuilder.BuildTreeView(something);
更酷的是,如果你使用超过3.5的.NET框架,你可以将该方法作为扩展:
public static class TreeViewBuilder
{
public static TreeView BuildTreeView( this TreeView tree )
{
DataTable projects = GetProjects();
DataTable releases = GetRealeases();
return InternalTreeViewBuilder(tree, projects, releases);
}
}
然后,调用你的方法会简单得多:
//TreeView something;
something.BuildTreeView();
它已经完成了! MSDN有一些很好的扩展方法代码。
希望我能帮忙!