2011-07-14 71 views
1

我正在學習C#編程語言,並且正在爲SAP業務One製作薪資應用程序附加組件。我以前從未使用樹視圖,並想知道如何從數據庫填充樹項目。我使用Visual Studio 2010和Microsoft SQL Server 2008C#從數據庫結果中填充樹視圖

我有一個父有2個孩子,即

- Component      ....Parent 
     Earnings      ....child 
     Deductions     ....child 

我想要的收益孩子表現出從U_PD_description領域的所有結果,其中U_PD_type =「收益「,即

- Component      ....Parent 
      Earnings      ....child 
       Housing Allowance 
       Mobile Phone Allowance 
       Mileage Allowance 
      Deductions     ....child 

並且同樣用於扣除。我有以下代碼formload:

private void frm_earn_deduct_setup_Load(object sender, EventArgs e) 
     { 
      // Get service instance 
      var earnDeductMasterService = Program.Kernel.Get<IEarnDeductMasterService>(); 

      //Query database for all records that have earnings 
      var earnings = from ed in earnDeductMasterService.GetAllEarnDeductMasters() 
          where ed.U_PD_type.Trim().Equals("Earnings".Trim(), StringComparison.CurrentCultureIgnoreCase) 
          select ed; 

      if (earnings.Any(x => x != null)) 
      { 
       //To populate subtree Earnings with U_PD_description results 
       //.....some code here 
      } 
      else 
      { 
       //Nothing to populate    
      } 

      //............................................................................. 
      //Query database for all records that have deductions 
      var deductions = from ed in earnDeductMasterService.GetAllEarnDeductMasters() 
          where ed.U_PD_type.Trim().Equals("Deductions".Trim(), StringComparison.CurrentCultureIgnoreCase) 
          select ed; 

      if (deductions.Any(x => x != null)) 
      { 
       //To populate subtree Deductions with U_PD_description results 
       //.....some code here 
      } 
      else 
      { 
       //Nothing to populate    
      } 

      // Disable default items 
      txt_amt_greater_than.Enabled = false; 
      bindingNavigatorDeleteItem.Enabled = false; 

      // Call service instance 
      earnDeductMasterBindingSource.DataSource = Program.Kernel.Get<IEarnDeductMasterService>().GetAllEarnDeductMasters().ToList(); 
     } 

誰能告訴我如何填充說,在爲TreeView1盈利樹的一個例​​子嗎?

回答

2

如果我理解正確的,你想要什麼,然後我這裏是如何填充樹視圖:

List<string> earnings = new List<string>() { "Housing Allowance", "Mobile Phone Allowance", "Mileage Allowance" }; 
List<string> deductions = new List<string>() { "Housing Ban", "Mobile Phone Ban", "Mileage Ban" }; 

treeView1.Nodes.Add("Component");//adding root node 
treeView1.Nodes[0].Nodes.Add("Earnings");//adding earnings child node 
treeView1.Nodes[0].Nodes.Add("Deductions");//adding deduction child node 

//adding all earnings to "Earnings" node 
foreach (string earning in earnings) 
{ 
    treeView1.Nodes[0].Nodes[0].Nodes.Add(earning); 
} 

//adding all deductions to "Deductions" node 
foreach (string deduction in deductions) 
{ 
    treeView1.Nodes[0].Nodes[1].Nodes.Add(deduction); 
} 
+0

非常感謝!住房補貼,手機記錄不會是靜態的。所以我想我必須從結果中創建一個列表 –

+0

是的,你已經有了它們只是爲每個循環循環並添加到teeview節點。我只是舉了一個例子。 – Reniuz

+0

好吧,知道了Renuiz,再次感謝 –

0

而是硬編碼序數值的,我寧願獲取數據的XML,然後遍歷XML,遞歸來填充樹狀