2013-05-06 171 views
0

我正在閱讀本文,嘗試使用1個表在WPF中創建2級深層次結構,C#使用Linq to Sql。該表沒有父鍵或子鍵關係。這如何在樹視圖中創建?使用Linq創建層次結構

http://www.scip.be/index.php?Page=ArticlesNET09

Here is the data from the db

我試圖讓紙第一級和標題樹的第二個。

DC12 
    Once Around  
    Second Around 
    New Age#3 
    Third Around 
DC13 
    New Age 

DC14 
    Rock & Roll 
    Rock & Roll #2 
DC15 
    Top 5 
    New Age #2 

這裏是類的

public class Parent 
{ 
    public string Paper { get; set; } 
    public IEnumerable<Child> Readings { get; set; } 
} 

public class Child 
{ 
    public string Paper { get; set; } 
    public string Title { get; set; } 
    public IEnumerable<Book> Books { get; set; } 
} 

回答

1

試試這個簡單的代碼(樣品從我的項目):

1的.aspx

<table> 
    <tr> 
     <td> 
     <asp:TreeView ID="HierarchyTreeView" ExpandDepth="3" PopulateNodesFromClient="true" 
                  ForeColor="Blue" BackColor="ButtonFace" ShowLines="true" ShowExpandCollapse="true" 
                  runat="server" OnTreeNodePopulate="HierarchyTreeView_TreeNodePopulate" /> 
     </td> 
    </tr> 
</table> 

2的.cs

protected void Page_Load(object sender, EventArgs e){ 
    this.PopulateRootLevel(); 
    } 

    private void PopulateRootLevel() 
    { 
     DataTable _dataTable = this.GetDataTable(ApplicationConfig.ConnString, "sp_PopulateRootLevel", "parentID", String.Empty); 
     this.PopulateNodes(_dataTable, this.HierarchyTreeView.Nodes); 
    } 

    private void PopulateSubLevel(String _parentID, TreeNode _parentNode) 
    { 
     DataTable _dataTable = this.GetDataTable(ApplicationConfig.ConnString, "sp_PopulateRootLevel", "parentID", _parentID); 
     this.PopulateNodes(_dataTable, _parentNode.ChildNodes); 
    } 

    protected void HierarchyTreeView_TreeNodePopulate(object sender, TreeNodeEventArgs e) 
    { 
     PopulateSubLevel(e.Node.Value.ToString(), e.Node); 
    } 

    private void PopulateNodes(DataTable _dataTable, TreeNodeCollection _nodes) 
    { 
     foreach (DataRow _dataRow in _dataTable.Rows) 
     { 
      TreeNode _treeNode = new TreeNode(); 
      _treeNode.Text = _dataRow["EmpName"].ToString(); 
      _treeNode.Value = _dataRow["EmpNumb"].ToString(); 

      if (_dataRow["FgActive"].ToString() == "Y") 
       _nodes.Add(_treeNode); 

      _treeNode.PopulateOnDemand = ((int)(_dataRow["ChildNodeCount"]) > 0); 
     } 
    } 

public DataTable GetDataTable(String _prmConnString, String _prmStoreProcedure, String _prmField, String _prmValue) 
    { 
     DataTable _result = new DataTable(); 

     string[] _field = _prmField.Split('|'); 
     string[] _value = _prmValue.Split('|'); 

     try 
     { 
      SqlConnection _conn = new SqlConnection(_prmConnString); 
      SqlCommand _cmd = new SqlCommand(); 
      _cmd.CommandType = CommandType.StoredProcedure; 
      _cmd.Parameters.Clear(); 
      _cmd.Connection = _conn; 
      _cmd.CommandTimeout = 180; 
      _cmd.CommandText = _prmStoreProcedure; 
      for (int i = 0; i < _field.Count(); i++) 
       _cmd.Parameters.AddWithValue("@" + _field[i], _value[i]); 

      SqlDataAdapter _da = new SqlDataAdapter(); 
      _da.SelectCommand = _cmd; 
      _da.Fill(_result); 
     } 
     catch (Exception ex) 
     { 
     } 

     return _result; 
    } 

3 .sql

CREATE PROC dbo.sp_PopulateRootLevel(@parentID VARCHAR(50)) 
AS 
SELECT a.EmpNumb,a.EmpName,a.CompanyID, b.JobTitleName + ' - ' + c.JobLevelName AS JobTitleLevel, 
(SELECT COUNT(*) FROM dbo.MsEmployee WHERE EmpNumbParent = a.EmpNumb) AS ChildNodeCount,a.FgActive 
FROM dbo.MsEmployee a 
LEFT JOIN dbo.MsJobTitle b ON a.JobTitle = b.JobTitleId 
LEFT JOIN dbo.MsJobLevel c ON a.JobLevel = c.JobLevelId 
WHERE a.EmpNumbParent = @parentID