2010-11-11 84 views
0

我有下面的代碼,我想把全局方法設置爲靜態,這樣我就可以在我的asp.net web應用程序上構建一對夫婦樹視圖。現在我使用下面的代碼構建自己的樹視圖。我正在考慮製作一個全局靜態方法來生成像這樣的節點結構,然後將它們分配給我的頁面中的樹形視圖......或類似的東西。我dotn關心實際解決方案的細節,只要我可以有一個方法調用像「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

我沒有得到這個問題的時候了,但我猜你正在尋找這樣的事情:

創建一個名爲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); 

更冷靜,如果你正在使用.NET Framework 3.5以上就可以使該方法作爲擴展:

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有一些很好的擴展方法代碼。

希望我能幫上忙!

+0

問:我的頁面上的樹視圖會有一個代碼,它會使用這個構造器方法,並使用以下語法:MyTree.BuildTreeView(); ?? – kacalapy 2010-11-11 16:32:54